﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.ElasticSearch;
using AbpBuild;
using AbpBuild.LawManage;
using AbpBuild.WenShu;
using AbpBuild.WenShu.Dto;
using Newtonsoft.Json.Linq;

using System;
using DotnetSpider.Selector;
using System.Collections;
using Nest;
using System.Text.RegularExpressions;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.EntityFrameworkCore;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Abp.Json;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Abp.Domain.Uow;
using System.Security.Policy;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using AbpBuild.BaseData;
using Castle.Core.Internal;
using Abp.Runtime.Caching;
using Abp.Application.Services;

namespace AbpBuild.WenShu
{
    /// <summary>
    /// 查询类
    /// </summary>
    public class WenShuSearshAppService : AbpBuildAppServiceBase, IWenShuSearshAppService
    {
        //private readonly IRepository<record, string> _recordrepository; //in the second db
        //private readonly IRepository<changemima, int> _changemima; //in the second db
        //private readonly IRepository<courtFullName, string> _courtfullnamey; //in the second db
        private readonly ICacheManager _cacheManager;

        private BaseDatasHelper _baseDatasHelper;
        private AbpElasticSearch _abpElasticSearch;
        private Helpers _Helpers;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="recordrepository"></param>
        /// <param name="changemima"></param>
        /// <param name="courtfullname"></param>
        /// <param name="abpElasticSearch"></param>
        /// <param name="baseDatasHelper"></param>
        /// <param name="helpers"></param>
        public WenShuSearshAppService(
            //IRepository<Law, string> lawrepository,
            //IRepository<record, string> recordrepository,
            //IRepository<changemima, int> changemima,
            //IRepository<courtFullName, string> courtfullname,
            AbpElasticSearch abpElasticSearch,
            BaseDatasHelper baseDatasHelper,
            Helpers helpers,
            ICacheManager cacheManager


        )
        {
            //_lawrepository = lawrepository;
            _abpElasticSearch = abpElasticSearch;
            //_recordrepository = recordrepository;
            //_changemima = changemima;
            //_courtfullnamey = courtfullname;
            _baseDatasHelper = baseDatasHelper;
            _Helpers = helpers;
            _cacheManager = cacheManager;
        }
      
        /// <summary>
        /// 首页 ：案例数/更新时间，法律法规/更新时间，公司/更新时间，个人/更新时间
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<RecordAndLaw> RecordAndLawCountAsync()
        {
            try
            {
                string cachestr = "RecordAndLawCount";
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null)
                {
                    var request = new SearchRequest("cprecord")
                    {

                    };
                    var law = new SearchRequest("law")
                    {

                    };
                    long ss = await _abpElasticSearch.CountAsync("cprecord", request.Query);
                    RecordAndLaw recordandlaw = new RecordAndLaw { cprecord = ss,
                        cprecorddate = DateTime.Now.ToString("yyyy-MM-dd"),
                        law = await _abpElasticSearch.CountAsync("law", law.Query),
                        lawdate = DateTime.Now.ToString("yyyy-MM-dd"),
                        company = 65015400 + (int)(ss/10),
                        companydate = DateTime.Now.ToString("yyyy-MM-dd"),
                        people = 32423570 + (int)(ss / 10),
                        peopledate = DateTime.Now.ToString("yyyy-MM-dd"),
                    };
                    _cacheManager.GetCache("WenShuSearsh").Set(cachestr, recordandlaw,null, new TimeSpan(1, 0, 0, 0));
                    return recordandlaw;
                }
                else
                {
                    return (RecordAndLaw)results;

                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        /// <summary>
        /// 法律明细中，每个一个法条被引用数量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<long> RecordLawCountAsync(RecordSearchParamDto input)
        {
            try
            {
                var request = new SearchRequest("cprecord")
                {
                    Size = 0,
                };
                QueryContainer querycontainer = SearchParem(input);
                if (querycontainer != null)
                {
                    request.Query = querycontainer;
                }
                return await _abpElasticSearch.CountAsync("cprecord", request.Query);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public class RecordAndLaw
        {
            public long cprecord { get; set; }
            public string cprecorddate { get; set; }
            public long law { get; set; }
            public string lawdate { get; set; }
            public long company { get; set; }
            public string companydate { get; set; }
            public long people { get; set; }
            public string peopledate { get; set; }
        }

        #region 查询
        /// <summary>
        /// 文书查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<RecordSearchOutParamDto> RecordSearchAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "RecordSearch" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    List<ISort> orders = new List<ISort>();
                    if (!string.IsNullOrEmpty(input.sortorder))
                    {
                        ISort sort = new FieldSort() { Field = input.sortorder, Order = (Nest.SortOrder?)input.Ascending };
                        orders.Add(sort);
                    }
                    else
                    {
                        ISort sort = new FieldSort() { Field = "judgeDate", Order = (Nest.SortOrder?)input.Ascending, UnmappedType = FieldType.Date };
                        orders.Add(sort);
                    }
                    //sort
                    string[] highField = new string[] { "id", "name", "docCode", "docCodeOthers", "peoples", "caseReason", "judgeType", "judgeProcess", "judgeDate", "courtFullName", "docType"
                        ,  "judgeZ", "judges", "yuangaoLv", "beigaoLv", "di3renLv", "courtClaim", "judgeResult" , "lawLists" };
                                                                                                                 
                    if (input.includeFields == null)
                    {
                        input.includeFields = highField;
                    }

                    if (input.size > 20)
                    {
                        input.size = 20;
                    }

                    var request = new SearchRequest("cprecord")
                    {
                        From = input.skip,
                        Size = input.size,
                        Source = new SourceFilter()
                        {
                            Includes = input.includeFields
                            //Includes = new string[] { "eventid", "eventname" },
                            //Excludes = new string[] { "roginalid", "description" }
                        },
                        Sort = orders,
                        TrackTotalHits = true
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode) 
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0 )
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode) 
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0) 
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0 )
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
            #region bangzhu

            //Fields fd1 = new string[] { "di3ren.gongsi", "di3ren.dailiren" };
            //qc = qc || new NestedQuery()
            //{
            //    Path = "di3ren",
            //    Query = new MultiMatchQuery
            //    {
            //        Query = item,
            //        Fields = fd1,
            //        Operator = Operator.And,
            //        Analyzer = "ik_max_word"
            //    }
            //};


            //request.Query = request.Query ||
            //    new MatchQuery
            //    {
            //        Query = keyword,
            //        Field = new Field("title"),
            //        Operator = Operator.And,
            //        Analyzer = "ik_smart"
            //    } || new NestedQuery()
            //    {
            //        Path = "lawrecorddetailinputdtos",
            //        Query = new MatchQuery
            //        {
            //            Query = keyword,
            //            Field = new Field("lawrecorddetailinputdtos.text"),
            //            Operator = Operator.And,
            //            Analyzer = "ik_smart"
            //        }
            //    } || new IdsQuery { } || new PrefixQuery { }|| new DateRangeQuery() { }|| new LongRangeQuery() { }||new TermQuery() { }||new TermsQuery() { }
            //    || new WildcardQuery() { } 代表任意（包括0个）多个字符? 代表任意一个字符
            //new IntervalsQuery

            //new TermQuery
            //{
            //    Field = "title",
            //    Value = keyword
            //};
            //(new MatchQuery
            //    {
            //        Query = keyword,
            //        Field = new Field("industry_labels"),
            //        Operator = Operator.And,
            //        Analyzer = "ik_smart"
            //    } || new MatchQuery
            //    {
            //        Query = keyword,
            //        Field = new Field("title"),
            //        Operator = Operator.And,
            //        Analyzer = "ik_smart"
            //    }) && new DateRangeQuery
            //    {
            //        Field = new Field("posting_date"),
            //        GreaterThanOrEqualTo = DateMath.FromString("2017-1-1")
            //    } && new NestedQuery()
            //    {
            //        Path = "lawrecorddetailinputdtos",
            //        Query = new MatchQuery
            //        {
            //            Query = keyword,
            //            Field = new Field("lawrecorddetailinputdtos.text"),
            //            Operator = Operator.And,
            //            Analyzer = "ik_smart"
            //        }

            //    };
            //BoolQuery bq = new BoolQuery()
            //{
            //Should = //多个QueryContainer
            //Must =
            //MustNot = 
            //}



            //SearchDescriptor<LawRecordInputDTO> query = new SearchDescriptor<LawRecordInputDTO>();
            //query.PostFilter(x => x
            //    .MultiMatch(s => s.
            //                    Fields(fs => fs
            //                        .Field(f => f.PathName)
            //                        .Field(f => f.Content)
            //                        ).
            //                    Query(keyword).
            //                    Operator(Operator.And).
            //                    Analyzer("ik_smart")
            //                 )
            //);
            //query.PostFilter(x => x
            //    .Term(t => t
            //        .Field(f => f.LawID)
            //        .Value(keyword)
            //        )
            //);
            //query.PostFilter(x => x.
            //    MatchPhrase(p => p.
            //        Field(f => f.PathName).
            //        Query(keyword)
            //    ));
            //query.PostFilter(x => x
            //    .DateRange(t => t
            //        .Field(f => f.PostDate)
            //        .GreaterThan(DateMath.FromString("2018-1-1T00:00:00"))
            //        )
            //    );
            //query.Query(q => q
            //    .Bool(b => b
            //        .Must(m => m
            //            .MultiMatch(s => s
            //                .Fields(fs => fs
            //                    .Field(f => f.Message)
            //                    )
            //                .Query("NEST")
            //                )
            //            )
            //        .MustNot(m => m
            //            .QueryString(qs => qs
            //                .Fields(fs => fs
            //                    .Field(f => f.User)
            //                    )
            //                .Query("nico")
            //                )
            //            )
            //        )
            //    );
            //query.PostFilter(x => x
            //    .DateRange(t => t
            //        .Field(f => f.PostDate)
            //        .GreaterThan(DateMath.FromString("2018-1-1T00:00:00"))
            //        )
            //    );
            //query.PostFilter(x => x
            //    .QueryString(t => t.
            //        Fields(f => f.
            //            Field(obj => obj.User)
            //        ).Query("wenli yswenli"))
            //    );

            //query.PostFilter(x => x
            //    .Range(t => t.
            //        Field(obj => obj.Type).
            //        GreaterThanOrEquals(1).
            //        LessThanOrEquals(100))
            //    );
            //query.Sort(x => x.
            //    Field("_score", Nest.SortOrder.Descending)
            //    );

            //var his = await _abpElasticSearch.SearchAsync<LawRecordInputDTO, string>("law", query);
            //return his.Hits.ToJsonString();
            #endregion
        }
        #endregion

        #region RecordSearchForAggAsync  4  4  3  4  4

        //public async Task<RecordSearchOutParamDto> RecordSearchForAggAsync(RecordSearchParamDto input)
        //{
        //    try
        //    {
        //        string cachestr = "RecordSearchForAgg" + GetCacheno(input);
        //        var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
        //        if (results == null || !input.iscache)
        //        {
        //            var request = new SearchRequest("cprecord")
        //            {
        //                Size = 0,
        //                Aggregations = new AggregationDictionary(), //聚合
        //            };

        //            if (input.isagg)
        //            {
        //                #region//聚合
        //                List<TermsOrder> ordercourtLevel = new List<TermsOrder>();
        //                ordercourtLevel.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggcourtLevel = new TermsAggregation("aggcourtLevel")
        //                {
        //                    //Field = "courtLevel",
        //                    Script = new InlineScript("doc['courtLevel']"),
        //                    Size = 10,
        //                    Order = ordercourtLevel,
        //                };
        //                request.Aggregations.Add("aggcourtLevel", aggcourtLevel);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderyearmoth = new List<TermsOrder>();
        //                orderyearmoth.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggyearmoth = new TermsAggregation("aggyearmoth")
        //                {
        //                    //Field = "yearmoth",
        //                    Script = new InlineScript("doc['yearmoth']"),
        //                    Size = 6,
        //                    Order = orderyearmoth,
        //                };
        //                request.Aggregations.Add("aggyearmoth", aggyearmoth);
        //                ////////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderdocType = new List<TermsOrder>();
        //                orderdocType.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggdocType = new TermsAggregation("aggdocType")
        //                {
        //                    //Field = "docType",
        //                    Script = new InlineScript("doc['docType']"),
        //                    Size = 10,
        //                    Order = orderdocType,
        //                };
        //                request.Aggregations.Add("aggdocType", aggdocType);
        //                ////////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderjudgeZ = new List<TermsOrder>();
        //                orderjudgeZ.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggjudgeZ = new TermsAggregation("aggjudgeZ")
        //                {
        //                    //Field = "judgeZ",
        //                    Script = new InlineScript("doc['judgeZ']"),
        //                    Size = 10,
        //                    Order = orderjudgeZ,
        //                };
        //                request.Aggregations.Add("aggjudgeZ", aggjudgeZ);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderjudges = new List<TermsOrder>();
        //                orderjudges.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggjudges = new TermsAggregation("aggjudges")
        //                {
        //                    //Field = "judges",
        //                    Script = new InlineScript("doc['judges']"),
        //                    Size = 10,
        //                    Order = orderjudges,
        //                };
        //                request.Aggregations.Add("aggjudges", aggjudges);

        //                #endregion
        //            }

        //            ///////////////////////
        //            QueryContainer querycontainer = SearchParem(input);
        //            if (querycontainer != null)
        //            {
        //                request.Query = querycontainer;
        //            }

        //            var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

        //            aggs aggs = new aggs();

        //            Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

        //            foreach (var item in his.Aggregations.Keys)
        //            {
        //                var dd = his.Aggregations.GetValueOrDefault(item);
        //                string types = dd.GetType().Name;
        //                if (types == "SingleBucketAggregate")
        //                {
        //                    foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
        //                    {
        //                        List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                        foreach (var ts in ((Nest.BucketAggregate)sl).Items)
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                        agg.Add(item, templ);
        //                    }
        //                }
        //                else
        //                {
        //                    List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                    foreach (var ts in ((Nest.BucketAggregate)dd).Items)
        //                    {
        //                        string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
        //                        long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

        //                        Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                        tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                        templ.Add(tempd);
        //                    }
        //                    if (templ.Count > 0)
        //                    {
        //                        agg.Add(item, templ);
        //                    }
        //                }


        //            }

        //            aggs.agg = agg;

        //            RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
        //            returnenty.aggs = aggs;


        //            if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
        //                && (input.Data.peoples == null || input.Data.peoples.Count == 0)
        //                && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
        //                    && (input.Data.judger == null || input.Data.judger.Count == 0)
        //                    && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
        //            }
        //            else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
        //                || (input.Data.peoples != null && input.Data.peoples.Count > 0)
        //                || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
        //                || (input.Data.judger != null && input.Data.judger.Count > 0)
        //                || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
        //            }

        //            return returnenty;

        //        }
        //        else
        //        {
        //            return (RecordSearchOutParamDto)results;

        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }

        //}


        public async Task<object> RecordSearchForAggAsync(RecordSearchParamDto input)
        {
            try
            {
                var courtLevelAgg =  courtLevelScriptAggAsync(input);
                var yearmothAgg =  yearmothScriptAggAsync(input);
                var docTypeAgg =  docTypeScriptAggAsync(input);
                var judgeZAgg =  judgeZScriptAggAsync(input);
                var judgesAgg =  judgesScriptAggAsync(input);
                await Task.WhenAll(courtLevelAgg, yearmothAgg, docTypeAgg, judgeZAgg, judgesAgg);
                var obj = new
                {
                    courtLevelAgg = courtLevelAgg.Result.aggs.agg.Values,
                    yearmothAgg = yearmothAgg.Result.aggs.agg.Values,
                    docTypeAgg = docTypeAgg.Result.aggs.agg.Values,
                    judgeZAgg = judgeZAgg.Result.aggs.agg.Values,
                    judgesAgg = judgesAgg.Result.aggs.agg.Values
                };
                return obj;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> courtLevelScriptAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "courtLevelScriptAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        #region//聚合
                        List<TermsOrder> ordercourtLevel = new List<TermsOrder>();
                        ordercourtLevel.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggcourtLevel = new TermsAggregation("aggcourtLevel")
                        {
                            //Field = "courtLevel",
                            Script = new InlineScript("doc['courtLevel']"),
                            Size = 10,
                            Order = ordercourtLevel,
                        };
                        request.Aggregations.Add("aggcourtLevel", aggcourtLevel);

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;

                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> yearmothScriptAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "yearmothScriptAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        #region//聚合
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderyearmoth = new List<TermsOrder>();
                        orderyearmoth.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggyearmoth = new TermsAggregation("aggyearmoth")
                        {
                            //Field = "yearmoth",
                            Script = new InlineScript("doc['yearmoth']"),
                            Size = 6,
                            Order = orderyearmoth,
                        };
                        request.Aggregations.Add("aggyearmoth", aggyearmoth);
                        ////////////////////////////////////////////////////////////////////////////////////////////

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;

                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> docTypeScriptAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "docTypeScriptAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        #region//聚合
                        ////////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderdocType = new List<TermsOrder>();
                        orderdocType.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggdocType = new TermsAggregation("aggdocType")
                        {
                            //Field = "docType",
                            Script = new InlineScript("doc['docType']"),
                            Size = 10,
                            Order = orderdocType,
                        };
                        request.Aggregations.Add("aggdocType", aggdocType);

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;

                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> judgeZScriptAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "judgeZScriptAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        #region//聚合
                        List<TermsOrder> orderjudgeZ = new List<TermsOrder>();
                        orderjudgeZ.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggjudgeZ = new TermsAggregation("aggjudgeZ")
                        {
                            //Field = "judgeZ",
                            Script = new InlineScript("doc['judgeZ']"),
                            Size = 10,
                            Order = orderjudgeZ,
                        };
                        request.Aggregations.Add("aggjudgeZ", aggjudgeZ);
                        ////////////////////////////////////////////////////////////////////////////////////////
                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;

                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> judgesScriptAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "judgesScriptAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        #region//聚合
                        
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderjudges = new List<TermsOrder>();
                        orderjudges.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggjudges = new TermsAggregation("aggjudges")
                        {
                            //Field = "judges",
                            Script = new InlineScript("doc['judges']"),
                            Size = 10,
                            Order = orderjudges,
                        };
                        request.Aggregations.Add("aggjudges", aggjudges);

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;

                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        #endregion

        #region RecordSearchFor1AggAsync  16  27  
        //public async Task<RecordSearchOutParamDto> RecordSearchFor1AggAsync(RecordSearchParamDto input)
        //{
        //    try
        //    {
        //        string cachestr = "RecordSearchForAgg1" + GetCacheno(input);
        //        var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
        //        if (results == null || !input.iscache)
        //        {

        //            var request = new SearchRequest("cprecord")
        //            {
        //                Size = 0,
        //                Aggregations = new AggregationDictionary(), //聚合
        //            };

        //            if (input.isagg)
        //            {
        //                List<TermsOrder> orderlawLists = new List<TermsOrder>();
        //                orderlawLists.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer agglawLists = new NestedAggregation("agglawLists")
        //                {
        //                    Path = "lawLists",
        //                    Aggregations = new TermsAggregation("lawLists")
        //                    {
        //                        Size = 10,
        //                        Field = "lawLists.fgmcAgg",
        //                        //Script = new InlineScript("doc['lawLists.fgmcAgg'].value"),
        //                        Order = orderlawLists,
        //                    }
        //                };
        //                request.Aggregations.Add("agglawLists", agglawLists);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderlawListstkt = new List<TermsOrder>();
        //                orderlawListstkt.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer agglawListstkt = new NestedAggregation("agglawListstkt")
        //                {
        //                    Path = "lawLists",
        //                    Aggregations = new TermsAggregation("lawLists-tkt")
        //                    {
        //                        Size = 10,
        //                        Order = orderlawListstkt,
        //                        Script = new InlineScript("doc['lawLists.fgmcAgg'].value + '-'+ doc['lawLists.tkt'].value"),
        //                    }
        //                };
        //                request.Aggregations.Add("agglawListstkt", agglawListstkt);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                ///////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordercompanys = new List<TermsOrder>();
        //                ordercompanys.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggcompanys = new NestedAggregation("aggcompanys")
        //                {
        //                    Path = "companys",
        //                    Aggregations = new TermsAggregation("companys")
        //                    {
        //                        Size = 10,
        //                        Field = "companys.industry",
        //                        //Script = new InlineScript("doc['companys.industry'].value"),
        //                        Order = ordercompanys,
        //                    }
        //                };
        //                request.Aggregations.Add("aggcompanys", aggcompanys);
        //            }

        //            ///////////////////////
        //            QueryContainer querycontainer = SearchParem(input);
        //            if (querycontainer != null)
        //            {
        //                request.Query = querycontainer;
        //            }

        //            var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

        //            aggs aggs = new aggs();

        //            Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

        //            foreach (var item in his.Aggregations.Keys)
        //            {
        //                var dd = his.Aggregations.GetValueOrDefault(item);
        //                string types = dd.GetType().Name;
        //                if (types == "SingleBucketAggregate")
        //                {
        //                    foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
        //                    {
        //                        List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                        foreach (var ts in ((Nest.BucketAggregate)sl).Items)
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                        agg.Add(item, templ);
        //                    }
        //                }
        //                else
        //                {
        //                    List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                    foreach (var ts in ((Nest.BucketAggregate)dd).Items)
        //                    {
        //                        string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
        //                        long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

        //                        Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                        tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                        templ.Add(tempd);
        //                    }
        //                    if (templ.Count > 0)
        //                    {
        //                        agg.Add(item, templ);
        //                    }
        //                }


        //            }

        //            aggs.agg = agg;

        //            RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
        //            returnenty.aggs = aggs;

        //            if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
        //                && (input.Data.peoples == null || input.Data.peoples.Count == 0)
        //                && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
        //                    && (input.Data.judger == null || input.Data.judger.Count == 0)
        //                    && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
        //            }
        //            else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
        //                || (input.Data.peoples != null && input.Data.peoples.Count > 0)
        //                || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
        //                || (input.Data.judger != null && input.Data.judger.Count > 0)
        //                || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
        //            }
        //            return returnenty;
        //        }
        //        else
        //        {
        //            return (RecordSearchOutParamDto)results;

        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }

        //}
        public async Task<object> RecordSearchFor1AggAsync(RecordSearchParamDto input)
        {
            try
            {
                var lawListsfgmcAgg = lawListsfgmcAggAsync(input);
                var lawListstktAgg = lawListstktAggAsync(input);
                //var industryAgg = industryAggAsync(input);
                await Task.WhenAll(lawListsfgmcAgg, lawListstktAgg);
                var obj = new
                {
                    lawListsfgmcAgg = lawListsfgmcAgg.Result.aggs.agg.Values,
                    lawListstktAgg = lawListstktAgg.Result.aggs.agg.Values,
                    //industryAgg = industryAgg.Result.aggs.agg.Values,
                };
                return obj;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> lawListsfgmcAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "lawListsfgmcAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        List<TermsOrder> orderlawLists = new List<TermsOrder>();
                        orderlawLists.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer agglawLists = new NestedAggregation("agglawLists")
                        {
                            Path = "lawLists",
                            Aggregations = new TermsAggregation("lawLists")
                            {
                                Size = 10,
                                //Field = "lawLists.fgmcAgg",
                                Script = new InlineScript("doc['lawLists.fgmcAgg'].value"),
                                Order = orderlawLists,
                            }
                        };
                        request.Aggregations.Add("agglawLists", agglawLists);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> lawListstktAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "lawListstktAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                       
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderlawListstkt = new List<TermsOrder>();
                        orderlawListstkt.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer agglawListstkt = new NestedAggregation("agglawListstkt")
                        {
                            Path = "lawLists",
                            Aggregations = new TermsAggregation("lawLists-tkt")
                            {
                                Size = 10,
                                Order = orderlawListstkt,
                                Script = new InlineScript("doc['lawLists.fgmcAgg'].value + '-'+ doc['lawLists.tkt'].value"),
                            }
                        };
                        request.Aggregations.Add("agglawListstkt", agglawListstkt);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> industryAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "industryAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////////
                        ///////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordercompanys = new List<TermsOrder>();
                        ordercompanys.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggcompanys = new NestedAggregation("aggcompanys")
                        {
                            Path = "companys",
                            Aggregations = new TermsAggregation("companys")
                            {
                                Size = 10,
                                //Field = "companys.industry",
                                Script = new InlineScript("doc['companys.industry'].value"),
                                Order = ordercompanys,
                            }
                        };
                        request.Aggregations.Add("aggcompanys", aggcompanys);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion

        #region RecordSearchFor2AggAsync   7  12  5  8
        //public async Task<RecordSearchOutParamDto> RecordSearchFor22AggAsync(RecordSearchParamDto input)
        //{
        //    try
        //    {
        //        string cachestr = "RecordSearchFor2" + GetCacheno(input);
        //        var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
        //        if (results == null || !input.iscache)
        //        {

        //            var request = new SearchRequest("cprecord")
        //            {
        //                Size = 0,
        //                Aggregations = new AggregationDictionary(), //聚合
        //            };

        //            if (input.isagg)
        //            {
        //                List<TermsOrder> orderlvshishiwusuo = new List<TermsOrder>();
        //                orderlvshishiwusuo.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer agglvshishiwusuo = new NestedAggregation("agglvshishiwusuo")
        //                {
        //                    Path = "lvs",
        //                    Aggregations = new TermsAggregation("aggLvs-lvshishiwusuo")
        //                    {
        //                        //Field = "lvs.lvshishiwusuo",
        //                        Script = new InlineScript("doc['lvs.lvshishiwusuo'].value"),
        //                        Size = 10,
        //                        Order = orderlvshishiwusuo,
        //                        Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
        //                    }
        //                };
        //                request.Aggregations.Add("agglvshishiwusuo", agglvshishiwusuo);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderLvs = new List<TermsOrder>();
        //                orderLvs.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer agglvshi = new NestedAggregation("agglvshi")
        //                {
        //                    Path = "lvs",
        //                    Aggregations = new TermsAggregation("aggLvs-lvshi")
        //                    {
        //                        //Field = "lvs.lvshi",
        //                        Script = new InlineScript("doc['lvs.lvshi'].value + '-'+ doc['lvs.lvshishiwusuo'].value"),
        //                        Size = 10,
        //                        Order = orderLvs,
        //                        Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
        //                    }
        //                };
        //                request.Aggregations.Add("agglvshi", agglvshi);
        //                //////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderyuangao = new List<TermsOrder>();
        //                orderyuangao.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggyuangao = new NestedAggregation("aggyuangao")
        //                {
        //                    Path = "yuangao",
        //                    Aggregations = new TermsAggregation("aggyuangao-gongsi")
        //                    {
        //                        //Field = "yuangao.gongsi",
        //                        Script = new InlineScript("doc['yuangao.gongsi'].value"),
        //                        Size = 10,
        //                        Order = orderyuangao,
        //                        Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
        //                    }
        //                };
        //                request.Aggregations.Add("aggyuangao", aggyuangao);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderbeigao = new List<TermsOrder>();
        //                orderbeigao.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggbeigao = new NestedAggregation("aggbeigao")
        //                {
        //                    Path = "beigao",
        //                    Aggregations = new TermsAggregation("aggbeigao-gongsi")
        //                    {
        //                        //Field = "beigao.gongsi",
        //                        Script = new InlineScript("doc['beigao.gongsi'].value"),
        //                        Size = 10,
        //                        Order = orderbeigao,
        //                        Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
        //                    }
        //                };
        //                request.Aggregations.Add("aggbeigao", aggbeigao);
        //            }

        //            ///////////////////////
        //            QueryContainer querycontainer = SearchParem(input);
        //            if (querycontainer != null)
        //            {
        //                request.Query = querycontainer;
        //            }

        //            var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

        //            aggs aggs = new aggs();

        //            Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

        //            foreach (var item in his.Aggregations.Keys)
        //            {
        //                var dd = his.Aggregations.GetValueOrDefault(item);
        //                string types = dd.GetType().Name;
        //                if (types == "SingleBucketAggregate")
        //                {
        //                    foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
        //                    {
        //                        List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                        foreach (var ts in ((Nest.BucketAggregate)sl).Items)
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                        agg.Add(item, templ);
        //                    }
        //                }
        //                else
        //                {
        //                    List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                    foreach (var ts in ((Nest.BucketAggregate)dd).Items)
        //                    {
        //                        string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
        //                        long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

        //                        Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                        tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                        templ.Add(tempd);
        //                    }
        //                    if (templ.Count > 0)
        //                    {
        //                        agg.Add(item, templ);
        //                    }
        //                }


        //            }

        //            aggs.agg = agg;

        //            RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
        //            returnenty.aggs = aggs;

        //            if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
        //                && (input.Data.peoples == null || input.Data.peoples.Count == 0)
        //                && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
        //                    && (input.Data.judger == null || input.Data.judger.Count == 0)
        //                    && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
        //            }
        //            else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
        //                || (input.Data.peoples != null && input.Data.peoples.Count > 0)
        //                || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
        //                || (input.Data.judger != null && input.Data.judger.Count > 0)
        //                || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
        //            }
        //            return returnenty;
        //        }
        //        else
        //        {
        //            return (RecordSearchOutParamDto)results;

        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }

        //}
        public async Task<object> RecordSearchFor2AggAsync(RecordSearchParamDto input)
        {
            try
            {
                var lvshishiwusuoAgg = lvshishiwusuoAggAsync(input);
                var lvshiAgg = lvshiAggAsync(input);
                var yuangaoAgg = yuangaoAggAsync(input);
                var beigaoAgg = beigaoAggAsync(input);
                await Task.WhenAll(lvshishiwusuoAgg, lvshiAgg, yuangaoAgg, beigaoAgg);
                var obj = new
                {
                    lvshishiwusuoAgg = lvshishiwusuoAgg.Result.aggs.agg.Values,
                    lvshiAgg = lvshiAgg.Result.aggs.agg.Values,
                    yuangaoAgg = yuangaoAgg.Result.aggs.agg.Values,
                    beigaoAgg = beigaoAgg.Result.aggs.agg.Values,
                };
                return obj;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> lvshishiwusuoAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "lvshishiwusuoAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        List<TermsOrder> orderlvshishiwusuo = new List<TermsOrder>();
                        orderlvshishiwusuo.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer agglvshishiwusuo = new NestedAggregation("agglvshishiwusuo")
                        {
                            Path = "lvs",
                            Aggregations = new TermsAggregation("aggLvs-lvshishiwusuo")
                            {
                                //Field = "lvs.lvshishiwusuo",
                                Script = new InlineScript("doc['lvs.lvshishiwusuo'].value"),
                                Size = 10,
                                Order = orderlvshishiwusuo,
                                Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
                            }
                        };
                        request.Aggregations.Add("agglvshishiwusuo", agglvshishiwusuo);
                        //////////////////////////////////////////////////////////////////////////////////////////
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> lvshiAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "lvshiAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderLvs = new List<TermsOrder>();
                        orderLvs.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer agglvshi = new NestedAggregation("agglvshi")
                        {
                            Path = "lvs",
                            Aggregations = new TermsAggregation("aggLvs-lvshi")
                            {
                                //Field = "lvs.lvshi",
                                Script = new InlineScript("doc['lvs.lvshi'].value + '-'+ doc['lvs.lvshishiwusuo'].value"),
                                Size = 10,
                                Order = orderLvs,
                                Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
                            }
                        };
                        request.Aggregations.Add("agglvshi", agglvshi);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> yuangaoAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "yuangaoAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                       
                        //////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderyuangao = new List<TermsOrder>();
                        orderyuangao.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggyuangao = new NestedAggregation("aggyuangao")
                        {
                            Path = "yuangao",
                            Aggregations = new TermsAggregation("aggyuangao-gongsi")
                            {
                                //Field = "yuangao.gongsi",
                                Script = new InlineScript("doc['yuangao.gongsi'].value"),
                                Size = 10,
                                Order = orderyuangao,
                                Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
                            }
                        };
                        request.Aggregations.Add("aggyuangao", aggyuangao);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> beigaoAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "beigaoAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderbeigao = new List<TermsOrder>();
                        orderbeigao.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggbeigao = new NestedAggregation("aggbeigao")
                        {
                            Path = "beigao",
                            Aggregations = new TermsAggregation("aggbeigao-gongsi")
                            {
                                //Field = "beigao.gongsi",
                                Script = new InlineScript("doc['beigao.gongsi'].value"),
                                Size = 10,
                                Order = orderbeigao,
                                Exclude = new TermsExclude(".*X.*|\"\"|.*某.*")
                            }
                        };
                        request.Aggregations.Add("aggbeigao", aggbeigao);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        #endregion

        #region RecordSearchFor3AggAsync 10
        //public async Task<RecordSearchOutParamDto> RecordSearchFor3AggAsync(RecordSearchParamDto input)
        //{
        //    try
        //    {
        //        string cachestr = "RecordSearchFor3" + GetCacheno(input);
        //        var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
        //        if (results == null || !input.iscache)
        //        {

        //            var request = new SearchRequest("cprecord")
        //            {
        //                Size = 0,
        //                Aggregations = new AggregationDictionary(), //聚合
        //            };

        //            if (input.isagg)
        //            {
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderproofs = new List<TermsOrder>();
        //                orderproofs.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggproofs = new TermsAggregation("aggproofs")
        //                {
        //                    Field = "proofs",
        //                    //Script = new InlineScript("doc['proofs']"),
        //                    Size = 20,
        //                    Order = orderproofs,
        //                };
        //                request.Aggregations.Add("aggproofs", aggproofs);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderfeatures = new List<TermsOrder>();
        //                orderfeatures.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggfeatures = new TermsAggregation("aggfeatures")
        //                {
        //                    Field = "features",
        //                    //Script = new InlineScript("doc['features']"),
        //                    Size = 20,
        //                    Order = orderfeatures,
        //                };
        //                request.Aggregations.Add("aggfeatures", aggfeatures);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderfocuss = new List<TermsOrder>();
        //                orderfocuss.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggfocuss = new TermsAggregation("aggfocuss")
        //                {
        //                    Field = "focuss",
        //                    //Script = new InlineScript("doc['focuss']"),
        //                    Size = 20,
        //                    Order = orderfocuss,
        //                };
        //                request.Aggregations.Add("aggfocuss", aggfocuss);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordermainCriminal = new List<TermsOrder>();
        //                ordermainCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggmainCriminal = new TermsAggregation("aggmainCriminal")
        //                {
        //                    Field = "mainCriminal",
        //                    //Script = new InlineScript("doc['mainCriminal']"),
        //                    Size = 10,
        //                    Order = ordermainCriminal,
        //                };
        //                request.Aggregations.Add("aggmainCriminal", aggmainCriminal);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderfuCriminal = new List<TermsOrder>();
        //                orderfuCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggfuCriminal = new TermsAggregation("aggfuCriminal")
        //                {
        //                    Field = "fuCriminal",
        //                    //Script = new InlineScript("doc['fuCriminal']"),
        //                    Size = 10,
        //                    Order = orderfuCriminal,
        //                };
        //                request.Aggregations.Add("aggfuCriminal", aggfuCriminal);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderchangeCriminal = new List<TermsOrder>();
        //                orderchangeCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggchangeCriminal = new TermsAggregation("aggchangeCriminal")
        //                {
        //                    Field = "changeCriminal",
        //                    //Script = new InlineScript("doc['changeCriminal']"),
        //                    Size = 10,
        //                    Order = orderchangeCriminal,
        //                };
        //                request.Aggregations.Add("aggchangeCriminal", aggchangeCriminal);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordernoxingCriminal = new List<TermsOrder>();
        //                ordernoxingCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggnoxingCriminal = new TermsAggregation("aggnoxingCriminal")
        //                {
        //                    Field = "noxingCriminal",
        //                    //Script = new InlineScript("doc['noxingCriminal']"),
        //                    Size = 10,
        //                    Order = ordernoxingCriminal,
        //                };
        //                request.Aggregations.Add("aggnoxingCriminal", aggnoxingCriminal);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordercriminalLaw = new List<TermsOrder>();
        //                ordercriminalLaw.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggcriminalLaw = new TermsAggregation("aggcriminalLaw")
        //                {
        //                    Field = "criminalLaw",
        //                    //Script = new InlineScript("doc['criminalLaw']"),
        //                    Size = 10,
        //                    Order = ordercriminalLaw,
        //                };
        //                request.Aggregations.Add("aggcriminalLaw", aggcriminalLaw);
        //                //////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderstatusCriminal = new List<TermsOrder>();
        //                orderstatusCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggstatusCriminal = new TermsAggregation("aggstatusCriminal")
        //                {
        //                    Field = "statusCriminal",
        //                    //Script = new InlineScript("doc['statusCriminal']"),
        //                    Size = 10,
        //                    Order = orderstatusCriminal,
        //                };
        //                request.Aggregations.Add("aggstatusCriminal", aggstatusCriminal);
        //                ////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderdisadvantage = new List<TermsOrder>();
        //                orderdisadvantage.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggdisadvantage = new TermsAggregation("aggdisadvantage")
        //                {
        //                    Field = "disadvantage",
        //                    //Script = new InlineScript("doc['disadvantage']"),
        //                    Size = 10,
        //                    Order = orderdisadvantage,
        //                };
        //                request.Aggregations.Add("aggdisadvantage", aggdisadvantage);
        //            }

        //            ///////////////////////
        //            QueryContainer querycontainer = SearchParem(input);
        //            if (querycontainer != null)
        //            {
        //                request.Query = querycontainer;
        //            }

        //            var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

        //            aggs aggs = new aggs();

        //            Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

        //            foreach (var item in his.Aggregations.Keys)
        //            {
        //                var dd = his.Aggregations.GetValueOrDefault(item);
        //                string types = dd.GetType().Name;
        //                if (types == "SingleBucketAggregate")
        //                {
        //                    foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
        //                    {
        //                        List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                        foreach (var ts in ((Nest.BucketAggregate)sl).Items)
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                        agg.Add(item, templ);
        //                    }
        //                }
        //                else
        //                {
        //                    List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                    foreach (var ts in ((Nest.BucketAggregate)dd).Items)
        //                    {
        //                        string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
        //                        long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

        //                        Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                        tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                        templ.Add(tempd);
        //                    }
        //                    if (templ.Count > 0)
        //                    {
        //                        agg.Add(item, templ);
        //                    }
        //                }


        //            }

        //            aggs.agg = agg;

        //            RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
        //            returnenty.aggs = aggs;

        //            if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
        //                && (input.Data.peoples == null || input.Data.peoples.Count == 0)
        //                && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
        //                    && (input.Data.judger == null || input.Data.judger.Count == 0)
        //                    && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
        //            }
        //            else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
        //                || (input.Data.peoples != null && input.Data.peoples.Count > 0)
        //                || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
        //                || (input.Data.judger != null && input.Data.judger.Count > 0)
        //                || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
        //            }
        //            return returnenty;
        //        }
        //        else
        //        {
        //            return (RecordSearchOutParamDto)results;

        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }

        //}
        public async Task<object> RecordSearchFor3AggAsync(RecordSearchParamDto input)
        {
            try
            {
                var proofsAgg = proofsAggAsync(input);
                var featuresAgg = featuresAggAsync(input);
                var focussAgg = focussAggAsync(input);
                var mainCriminalAgg = mainCriminalAggAsync(input);
                var fuCriminalAgg = fuCriminalAggAsync(input);
                var changeCriminalAgg = changeCriminalAggAsync(input);
                var noxingCriminalAgg = noxingCriminalAggAsync(input);
                var criminalLawAgg = criminalLawAggAsync(input);
                var statusCriminalAgg = statusCriminalAggAsync(input);
                var disadvantageAgg = disadvantageAggAsync(input);
                var judgeresultsagg = judgeresultsaggasync(input);

                await Task.WhenAll(proofsAgg, featuresAgg, focussAgg, mainCriminalAgg, fuCriminalAgg, changeCriminalAgg, noxingCriminalAgg, criminalLawAgg, statusCriminalAgg, disadvantageAgg, judgeresultsagg);
                var obj = new
                {
                    proofsAgg = proofsAgg.Result.aggs.agg.Values,
                    featuresAgg = featuresAgg.Result.aggs.agg.Values,
                    focussAgg = focussAgg.Result.aggs.agg.Values,
                    mainCriminalAgg = mainCriminalAgg.Result.aggs.agg.Values,
                    fuCriminalAgg = fuCriminalAgg.Result.aggs.agg.Values,
                    changeCriminalAgg = changeCriminalAgg.Result.aggs.agg.Values,
                    noxingCriminalAgg = noxingCriminalAgg.Result.aggs.agg.Values,
                    criminalLawAgg = criminalLawAgg.Result.aggs.agg.Values,
                    statusCriminalAgg = statusCriminalAgg.Result.aggs.agg.Values,
                    disadvantageAgg = disadvantageAgg.Result.aggs.agg.Values,
                    judgeresultsagg = judgeresultsagg.Result.aggs.agg.Values
                };
                return obj;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> proofsAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "proofsAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderproofs = new List<TermsOrder>();
                        orderproofs.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggproofs = new TermsAggregation("aggproofs")
                        {
                            Field = "proofs",
                            //Script = new InlineScript("doc['proofs']"),
                            Size = 20,
                            Order = orderproofs,
                        };
                        request.Aggregations.Add("aggproofs", aggproofs);
                        ////////////////////////////////////////////////////////////////////////////////////////
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> featuresAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "featuresAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderfeatures = new List<TermsOrder>();
                        orderfeatures.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggfeatures = new TermsAggregation("aggfeatures")
                        {
                            Field = "features",
                            //Script = new InlineScript("doc['features']"),
                            Size = 20,
                            Order = orderfeatures,
                        };
                        request.Aggregations.Add("aggfeatures", aggfeatures);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> focussAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "focussAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                       
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderfocuss = new List<TermsOrder>();
                        orderfocuss.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggfocuss = new TermsAggregation("aggfocuss")
                        {
                            Field = "focuss",
                            //Script = new InlineScript("doc['focuss']"),
                            Size = 20,
                            Order = orderfocuss,
                        };
                        request.Aggregations.Add("aggfocuss", aggfocuss);
                        //////////////////////////////////////////////////////////////////////////////////////
                       
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> mainCriminalAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "mainCriminalAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordermainCriminal = new List<TermsOrder>();
                        ordermainCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggmainCriminal = new TermsAggregation("aggmainCriminal")
                        {
                            Field = "mainCriminal",
                            //Script = new InlineScript("doc['mainCriminal']"),
                            Size = 10,
                            Order = ordermainCriminal,
                        };
                        request.Aggregations.Add("aggmainCriminal", aggmainCriminal);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> fuCriminalAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "fuCriminalAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderfuCriminal = new List<TermsOrder>();
                        orderfuCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggfuCriminal = new TermsAggregation("aggfuCriminal")
                        {
                            Field = "fuCriminal",
                            //Script = new InlineScript("doc['fuCriminal']"),
                            Size = 10,
                            Order = orderfuCriminal,
                        };
                        request.Aggregations.Add("aggfuCriminal", aggfuCriminal);
                        //////////////////////////////////////////////////////////////////////////////////////
                        
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> changeCriminalAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "changeCriminalAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderchangeCriminal = new List<TermsOrder>();
                        orderchangeCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggchangeCriminal = new TermsAggregation("aggchangeCriminal")
                        {
                            Field = "changeCriminal",
                            //Script = new InlineScript("doc['changeCriminal']"),
                            Size = 10,
                            Order = orderchangeCriminal,
                        };
                        request.Aggregations.Add("aggchangeCriminal", aggchangeCriminal);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> noxingCriminalAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "noxingCriminalAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        ////////////////////////////////////////////////////////////////////////////////////////
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordernoxingCriminal = new List<TermsOrder>();
                        ordernoxingCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggnoxingCriminal = new TermsAggregation("aggnoxingCriminal")
                        {
                            Field = "noxingCriminal",
                            //Script = new InlineScript("doc['noxingCriminal']"),
                            Size = 10,
                            Order = ordernoxingCriminal,
                        };
                        request.Aggregations.Add("aggnoxingCriminal", aggnoxingCriminal);
                        
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> criminalLawAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "criminalLawAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordercriminalLaw = new List<TermsOrder>();
                        ordercriminalLaw.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggcriminalLaw = new TermsAggregation("aggcriminalLaw")
                        {
                            Field = "criminalLaw",
                            //Script = new InlineScript("doc['criminalLaw']"),
                            Size = 10,
                            Order = ordercriminalLaw,
                        };
                        request.Aggregations.Add("aggcriminalLaw", aggcriminalLaw);
                        
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> statusCriminalAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "statusCriminalAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        //////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderstatusCriminal = new List<TermsOrder>();
                        orderstatusCriminal.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggstatusCriminal = new TermsAggregation("aggstatusCriminal")
                        {
                            Field = "statusCriminal",
                            //Script = new InlineScript("doc['statusCriminal']"),
                            Size = 10,
                            Order = orderstatusCriminal,
                        };
                        request.Aggregations.Add("aggstatusCriminal", aggstatusCriminal);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> disadvantageAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "disadvantageAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {
                        
                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderdisadvantage = new List<TermsOrder>();
                        orderdisadvantage.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggdisadvantage = new TermsAggregation("aggdisadvantage")
                        {
                            Field = "disadvantage",
                            //Script = new InlineScript("doc['disadvantage']"),
                            Size = 10,
                            Order = orderdisadvantage,
                        };
                        request.Aggregations.Add("aggdisadvantage", aggdisadvantage);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> judgeresultsaggasync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "judgeresultsaggasync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {

                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    if (input.isagg)
                    {

                        ////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderjudgeResults = new List<TermsOrder>();
                        orderjudgeResults.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer judgeResults = new TermsAggregation("judgeResults")
                        {
                            Field = "judgeResults",
                            //Script = new InlineScript("doc['disadvantage']"),
                            Size = 10,
                            Order = orderjudgeResults,
                        };
                        request.Aggregations.Add("judgeResults", judgeResults);
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }

                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    aggs aggs = new aggs();

                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                if (key == "判决" || key == "调解" || key == "维持")
                                {
                                    continue;
                                }
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.aggs = aggs;

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }
                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion     1

        #region RecordSearchAsync4AggAsync  1  4  4  1  2  
        //public async Task<RecordSearchOutParamDto> RecordSearchAsync4AggAsync(RecordSearchParamDto input)
        //{
        //    try
        //    {
        //        string cachestr = "RecordSearchAsync4Agg" + GetCacheno(input);
        //        var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
        //        if (results == null || !input.iscache)
        //        {
        //            //关键词高亮
        //            var request = new SearchRequest("cprecord")
        //            {
        //                Size = 0,
        //                Aggregations = new AggregationDictionary(), //聚合
        //            };

        //            //正则表达式查询
        //            //RegexpQuery rq = new RegexpQuery();
        //            //rq.Field = "description";
        //            //rq.Value = "azu.*";
        //            //rq.MaximumDeterminizedStates = 20000;


        //            if (input.isagg)
        //            {
        //                #region//聚合

        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderyear = new List<TermsOrder>();
        //                orderyear.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggyear = new TermsAggregation("aggyear")
        //                {
        //                    Field = "year",
        //                    //Script = new InlineScript("doc['year'].value"),
        //                    Size = 10,
        //                    Order = orderyear,
        //                };
        //                request.Aggregations.Add("aggyear", aggyear);

        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderkeywords = new List<TermsOrder>();
        //                orderkeywords.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggkeywords = new TermsAggregation("aggkeywords")
        //                {
        //                    Field = "keywords",
        //                    //Script = new InlineScript("doc['keywords']"),
        //                    Size = 20,
        //                    Order = orderkeywords,
        //                };
        //                request.Aggregations.Add("aggkeywords", aggkeywords);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordercaseReason = new List<TermsOrder>();
        //                ordercaseReason.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
        //                AggregationContainer aggcaseReason = new TermsAggregation("aggcaseReason")
        //                {
        //                    Field = "caseReason",
        //                    //Script = new InlineScript("doc['caseReason']"),
        //                    Size = 2000,
        //                    Order = ordercaseReason,

        //                };
        //                request.Aggregations.Add("aggcaseReason", aggcaseReason);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderjudgeType = new List<TermsOrder>();
        //                orderjudgeType.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggjudgeType = new TermsAggregation("aggjudgeType")
        //                {
        //                    Field = "judgeType",
        //                    //Script = new InlineScript("doc['judgeType']"),
        //                    Size = 100,
        //                    Order = orderjudgeType,
        //                };
        //                request.Aggregations.Add("aggjudgeType", aggjudgeType);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderjudgeProcess = new List<TermsOrder>();
        //                orderjudgeProcess.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggjudgeProcess = new TermsAggregation("aggjudgeProcess")
        //                {
        //                    Field = "judgeProcess",
        //                    //Script = new InlineScript("doc['judgeProcess']"),
        //                    Size = 1000,
        //                    Order = orderjudgeProcess,
        //                };
        //                request.Aggregations.Add("aggjudgeProcess", aggjudgeProcess);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> ordercourtFullName = new List<TermsOrder>();
        //                ordercourtFullName.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggcourtFullName = new TermsAggregation("aggcourtFullName")
        //                {
        //                    Field = "courtFullNameAgg",
        //                    //Script = new InlineScript("doc['courtFullNameAgg'].value"),
        //                    Size = 1000,
        //                    Order = ordercourtFullName,
        //                };
        //                request.Aggregations.Add("aggcourtFullName", aggcourtFullName);
        //                //////////////////////////////////////////////////////////////////////////////////////////
        //                List<TermsOrder> orderregion = new List<TermsOrder>();
        //                orderregion.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
        //                AggregationContainer aggregion = new TermsAggregation("aggregion")
        //                {
        //                    Field = "region",
        //                    //Script = new InlineScript("doc['region']"),
        //                    Size = 20,
        //                    Order = orderregion,
        //                };
        //                request.Aggregations.Add("aggregion", aggregion);

        //                #endregion
        //            }

        //            ///////////////////////
        //            QueryContainer querycontainer = SearchParem(input);
        //            if (querycontainer != null)
        //            {
        //                request.Query = querycontainer;
        //            }



        //            var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

        //            string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
        //            List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
        //            List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
        //            foreach (var item in his.Hits)
        //            {
        //                RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
        //                if (!string.IsNullOrEmpty(enty.Id))
        //                {
        //                    _Helpers.ReturnhightTags(enty, x);
        //                }
        //                recordsearchoutdtos.Add(enty);
        //            }

        //            aggs aggs = new aggs();
        //            List<caseReason> crs = _baseDatasHelper.GetcaseReasonOUT(null, null);
        //            List<LawjudgeProcess> ljps = _baseDatasHelper.GetjudgeProcessOUT(null, null);
        //            List<region> re = new List<region>();
        //            List<courtFullName> cfs = _baseDatasHelper.GetcourtFullName("", "", 0);
        //            List<courtFullName> tempss = new List<courtFullName>();
        //            Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

        //            foreach (var item in his.Aggregations.Keys)
        //            {
        //                var dd = his.Aggregations.GetValueOrDefault(item);
        //                string types = dd.GetType().Name;
        //                if (types == "SingleBucketAggregate")
        //                {
        //                    foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
        //                    {
        //                        List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                        foreach (var ts in ((Nest.BucketAggregate)sl).Items)
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                        agg.Add(item, templ);
        //                    }
        //                }
        //                else
        //                {
        //                    List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
        //                    foreach (var ts in ((Nest.BucketAggregate)dd).Items)
        //                    {
        //                        string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
        //                        long? count = ((Nest.KeyedBucket<object>)ts).DocCount;
        //                        if (item == "aggcaseReason")
        //                        {
        //                            if (key.Split('-').Count() > 1)
        //                            {
        //                                caseReason cr = crs.Where(p => p.Id == key.Split('-')[1]).FirstOrDefault();
        //                                if (cr != null)
        //                                {
        //                                    cr.count = count;
        //                                }
        //                            }
        //                            else
        //                            {
        //                                caseReason cr = crs.Where(p => p.text == key).FirstOrDefault();
        //                                if (cr != null)
        //                                {
        //                                    cr.count = count;
        //                                }
        //                            }
        //                        }
        //                        else if (item == "aggjudgeType")
        //                        {
        //                            LawjudgeProcess cr = ljps.Where(p => p.name == key).FirstOrDefault();
        //                            if (cr != null)
        //                            {
        //                                cr.count = count;
        //                            }
        //                        }
        //                        else if (item == "aggjudgeProcess")
        //                        {
        //                            LawjudgeProcess cr = ljps.Where(p => p.name == key).FirstOrDefault();
        //                            if (cr != null)
        //                            {
        //                                cr.count = count;
        //                            }
        //                        }
        //                        else if (item == "aggregion")
        //                        {
        //                            region reg = new region() { count = count, dataValue = key };
        //                            re.Add(reg);
        //                        }
        //                        else if (item == "aggcourtFullName")
        //                        {
        //                            courtFullName cf = cfs.Where(p => p.name == key).FirstOrDefault();
        //                            if (cf != null)
        //                            {
        //                                cf.count = count;
        //                                tempss.Add(cf);

        //                            }
        //                        }
        //                        else
        //                        {
        //                            Dictionary<string, long?> tempd = new Dictionary<string, long?>();
        //                            tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
        //                            templ.Add(tempd);
        //                        }
        //                    }
        //                    if (templ.Count > 0)
        //                    {
        //                        agg.Add(item, templ);
        //                    }
        //                }


        //            }

        //            foreach (var item in tempss)
        //            {
        //                region reg = re.Where(p => p.dataValue == item.region).FirstOrDefault();
        //                if (reg != null)
        //                {
        //                    reg.courts.Add(item);
        //                }
        //            }

        //            crs = crs.Where(p => p.count > 0).ToList();
        //            List<caseReason> crlevels = crs.Where(p => p.levels == 1).ToList();

        //            for (int i = 0; i < crlevels.Count; i++)
        //            {
        //                if (crlevels[i].childCasereasons != null && crlevels[i].childCasereasons.Count > 0)
        //                {
        //                    List<caseReason> crs1 = crlevels[i].childCasereasons.ToList();
        //                    for (int i1 = 0; i1 < crs1.Count; i1++)
        //                    {
        //                        caseReason crt1 = crs.Where(p => p.Id == crs1[i1].Id).FirstOrDefault();
        //                        if (crt1 != null)
        //                        {
        //                            crs1[i1].count = crt1.count;
        //                            if (crs1[i1].childCasereasons != null && crs1[i1].childCasereasons.Count > 0)
        //                            {
        //                                List<caseReason> crs2 = crs1[i1].childCasereasons.ToList();
        //                                for (int i2 = 0; i2 < crs2.Count; i2++)
        //                                {
        //                                    caseReason crt3 = crs.Where(p => p.Id == crs2[i2].Id).FirstOrDefault();
        //                                    if (crt3 != null)
        //                                    {
        //                                        crs2[i2].count = crt3.count;
        //                                        if (crs2[i2].childCasereasons != null && crs2[i2].childCasereasons.Count > 0)
        //                                        {
        //                                            List<caseReason> crs3 = crs2[i2].childCasereasons.ToList();
        //                                            for (int i3 = 0; i3 < crs3.Count; i3++)
        //                                            {
        //                                                caseReason crt4 = crs.Where(p => p.Id == crs3[i3].Id).FirstOrDefault();
        //                                                if (crt4 != null)
        //                                                {
        //                                                    crs3[i3].count = crt4.count;
        //                                                    if (crs3[i3].childCasereasons != null && crs3[i3].childCasereasons.Count > 0)
        //                                                    {
        //                                                        List<caseReason> crs4 = crs3[i3].childCasereasons.ToList();
        //                                                        for (int i4 = 0; i4 < crs4.Count; i4++)
        //                                                        {
        //                                                            caseReason crt5 = crs.Where(p => p.Id == crs4[i4].Id).FirstOrDefault();
        //                                                            if (crt5 != null)
        //                                                            {
        //                                                                crs4[i4].count = crt5.count;
        //                                                            }
        //                                                        }
        //                                                    }
        //                                                }
        //                                            }
        //                                        }
        //                                    }
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //            for (int i = 0; i < crlevels.Count; i++)
        //            {
        //                if (crlevels[i].count == null || crlevels[i].count == 0)
        //                {
        //                    crlevels.Remove(crlevels[i]);
        //                }
        //                else
        //                {
        //                    if (crlevels[i].childCasereasons != null && crlevels[i].childCasereasons.Count > 0)
        //                    {
        //                        List<caseReason> crs1 = crlevels[i].childCasereasons.ToList();
        //                        for (int i1 = 0; i1 < crs1.Count; i1++)
        //                        {
        //                            if (crs1[i1].count == null || crs1[i1].count == 0)
        //                            {
        //                                crlevels[i].childCasereasons.Remove(crs1[i1]);
        //                            }
        //                            else
        //                            {
        //                                if (crs1[i1].childCasereasons != null && crs1[i1].childCasereasons.Count > 0)
        //                                {
        //                                    List<caseReason> crs2 = crs1[i1].childCasereasons.ToList();
        //                                    for (int i2 = 0; i2 < crs2.Count; i2++)
        //                                    {
        //                                        if (crs2[i2].count == null || crs2[i2].count == 0)
        //                                        {
        //                                            crs1[i1].childCasereasons.Remove(crs2[i2]);
        //                                        }
        //                                        else
        //                                        {
        //                                            if (crs2[i2].childCasereasons != null && crs2[i2].childCasereasons.Count > 0)
        //                                            {
        //                                                List<caseReason> crs3 = crs2[i2].childCasereasons.ToList();
        //                                                for (int i3 = 0; i3 < crs3.Count; i3++)
        //                                                {
        //                                                    if (crs3[i3].count == null || crs3[i3].count == 0)
        //                                                    {
        //                                                        crs2[i2].childCasereasons.Remove(crs3[i3]);
        //                                                    }
        //                                                    else
        //                                                    {
        //                                                        if (crs3[i3].childCasereasons != null && crs3[i3].childCasereasons.Count > 0)
        //                                                        {
        //                                                            List<caseReason> crs4 = crs3[i3].childCasereasons.ToList();
        //                                                            for (int i4 = 0; i4 < crs4.Count; i4++)
        //                                                            {
        //                                                                if (crs4[i4].count == null || crs4[i4].count == 0)
        //                                                                {
        //                                                                    crs3[i3].childCasereasons.Remove(crs4[i4]);
        //                                                                }
        //                                                            }
        //                                                        }
        //                                                    }
        //                                                }

        //                                            }
        //                                        }
        //                                    }

        //                                }
        //                            }
        //                        }

        //                    }
        //                }
        //            }




        //            ljps = ljps.Where(p => p.count > 0).ToList();
        //            List<LawjudgeProcess> ljpLevels = ljps.Where(p => p.levels == 1).ToList();
        //            for (int i = 0; i < ljpLevels.Count; i++)
        //            {
        //                if (ljpLevels[i].childLawjudgeProcess != null && ljpLevels[i].childLawjudgeProcess.Count > 0)
        //                {
        //                    List<LawjudgeProcess> ljps1 = ljpLevels[i].childLawjudgeProcess.ToList();
        //                    for (int i1 = 0; i1 < ljps1.Count; i1++)
        //                    {
        //                        LawjudgeProcess crt1 = ljps.Where(p => p.Id == ljps1[i1].Id).FirstOrDefault();
        //                        if (crt1 != null)
        //                        {
        //                            ljps1[i1].count = crt1.count;
        //                            if (ljps1[i1].childLawjudgeProcess != null && ljps1[i1].childLawjudgeProcess.Count > 0)
        //                            {
        //                                List<LawjudgeProcess> ljps2 = ljps1[i1].childLawjudgeProcess.ToList();
        //                                for (int i2 = 0; i2 < ljps2.Count; i2++)
        //                                {
        //                                    LawjudgeProcess crt3 = ljps.Where(p => p.Id == ljps2[i2].Id).FirstOrDefault();
        //                                    if (crt3 != null)
        //                                    {
        //                                        ljps2[i2].count = crt3.count;
        //                                        if (ljps2[i2].childLawjudgeProcess != null && ljps2[i2].childLawjudgeProcess.Count > 0)
        //                                        {
        //                                            List<LawjudgeProcess> ljps3 = ljps2[i2].childLawjudgeProcess.ToList();
        //                                            for (int i3 = 0; i3 < ljps3.Count; i3++)
        //                                            {
        //                                                LawjudgeProcess crt4 = ljps.Where(p => p.Id == ljps3[i3].Id).FirstOrDefault();
        //                                                if (crt4 != null)
        //                                                {
        //                                                    ljps3[i3].count = crt4.count;
        //                                                    if (ljps3[i3].childLawjudgeProcess != null && ljps3[i3].childLawjudgeProcess.Count > 0)
        //                                                    {
        //                                                        List<LawjudgeProcess> ljps4 = ljps3[i3].childLawjudgeProcess.ToList();
        //                                                        for (int i4 = 0; i4 < ljps4.Count; i4++)
        //                                                        {
        //                                                            LawjudgeProcess crt5 = ljps.Where(p => p.Id == ljps4[i4].Id).FirstOrDefault();
        //                                                            if (crt5 != null)
        //                                                            {
        //                                                                ljps4[i4].count = crt5.count;
        //                                                            }
        //                                                        }
        //                                                    }
        //                                                }
        //                                            }
        //                                        }
        //                                    }
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //            for (int i = 0; i < ljpLevels.Count; i++)
        //            {
        //                if (ljpLevels[i].count == null || ljpLevels[i].count == 0)
        //                {
        //                    ljpLevels.Remove(ljpLevels[i]);
        //                }
        //                else
        //                {
        //                    if (ljpLevels[i].childLawjudgeProcess != null && ljpLevels[i].childLawjudgeProcess.Count > 0)
        //                    {
        //                        List<LawjudgeProcess> crs1 = ljpLevels[i].childLawjudgeProcess.ToList();
        //                        for (int i1 = 0; i1 < crs1.Count; i1++)
        //                        {
        //                            if (crs1[i1].count == null || crs1[i1].count == 0)
        //                            {
        //                                ljpLevels[i].childLawjudgeProcess.Remove(crs1[i1]);
        //                            }
        //                            else
        //                            {
        //                                if (crs1[i1].childLawjudgeProcess != null && crs1[i1].childLawjudgeProcess.Count > 0)
        //                                {
        //                                    List<LawjudgeProcess> crs2 = crs1[i1].childLawjudgeProcess.ToList();
        //                                    for (int i2 = 0; i2 < crs2.Count; i2++)
        //                                    {
        //                                        if (crs2[i2].count == null || crs2[i2].count == 0)
        //                                        {
        //                                            crs1[i1].childLawjudgeProcess.Remove(crs2[i2]);
        //                                        }
        //                                        else
        //                                        {
        //                                            if (crs2[i2].childLawjudgeProcess != null && crs2[i2].childLawjudgeProcess.Count > 0)
        //                                            {
        //                                                List<LawjudgeProcess> crs3 = crs2[i2].childLawjudgeProcess.ToList();
        //                                                for (int i3 = 0; i3 < crs3.Count; i3++)
        //                                                {
        //                                                    if (crs3[i3].count == null || crs3[i3].count == 0)
        //                                                    {
        //                                                        crs2[i2].childLawjudgeProcess.Remove(crs3[i3]);
        //                                                    }
        //                                                    else
        //                                                    {
        //                                                        if (crs3[i3].childLawjudgeProcess != null && crs3[i3].childLawjudgeProcess.Count > 0)
        //                                                        {
        //                                                            List<LawjudgeProcess> crs4 = crs3[i3].childLawjudgeProcess.ToList();
        //                                                            for (int i4 = 0; i4 < crs4.Count; i4++)
        //                                                            {
        //                                                                if (crs4[i4].count == null || crs4[i4].count == 0)
        //                                                                {
        //                                                                    crs3[i3].childLawjudgeProcess.Remove(crs4[i4]);
        //                                                                }
        //                                                            }
        //                                                        }
        //                                                    }
        //                                                }

        //                                            }
        //                                        }
        //                                    }

        //                                }
        //                            }
        //                        }

        //                    }
        //                }
        //            }



        //            aggs.agg = agg;
        //            aggs.crs = crlevels;
        //            aggs.ljps = ljpLevels;
        //            aggs.re = re;



        //            RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
        //            returnenty.Datas = recordsearchoutdtos;
        //            returnenty.aggs = aggs;
        //            returnenty.skip = input.skip;
        //            returnenty.size = input.size;
        //            returnenty.sortorder = input.sortorder;
        //            returnenty.Ascending = input.Ascending;
        //            returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

        //            if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
        //                && (input.Data.peoples == null || input.Data.peoples.Count == 0)
        //                && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
        //                    && (input.Data.judger == null || input.Data.judger.Count == 0)
        //                    && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
        //            }
        //            else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
        //                || (input.Data.peoples != null && input.Data.peoples.Count > 0)
        //                || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
        //                || (input.Data.judger != null && input.Data.judger.Count > 0)
        //                || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
        //            {
        //                _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
        //            }

        //            return returnenty;
        //        }
        //        else
        //        {
        //            return (RecordSearchOutParamDto)results;

        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }
        //}
        public async Task<object> RecordSearchAsync4AggAsync(RecordSearchParamDto input)
        {
            try
            {
                var yearAgg = yearAggAsync(input);
                var keywordsAgg = keywordsAggAsync(input);
                var caseReasonAgg = caseReasonAggAsync(input);
                var judgeTypeAgg = judgeTypeAggAsync(input);
                var courtFullNameAgg = courtFullNameAggAsync(input);
                await Task.WhenAll(yearAgg, keywordsAgg, caseReasonAgg, judgeTypeAgg, courtFullNameAgg);
                var obj = new
                {
                    yearAgg = yearAgg.Result.aggs.agg.Values,
                    keywordsAgg = keywordsAgg.Result.aggs.agg.Values,
                    caseReasonAgg = caseReasonAgg.Result.aggs.crs,
                    judgeTypeAgg = judgeTypeAgg.Result.aggs.ljps,
                    courtFullNameAgg = courtFullNameAgg.Result.aggs.re,
                };
                return obj;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> yearAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "yearAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合

                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderyear = new List<TermsOrder>();
                        orderyear.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggyear = new TermsAggregation("aggyear")
                        {
                            Field = "year",
                            //Script = new InlineScript("doc['year'].value"),
                            Size = 10,
                            Order = orderyear,
                        };
                        request.Aggregations.Add("aggyear", aggyear);


                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }

                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.aggs = aggs;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> keywordsAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "keywordsAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合

                       
                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderkeywords = new List<TermsOrder>();
                        orderkeywords.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggkeywords = new TermsAggregation("aggkeywords")
                        {
                            Field = "keywords",
                            //Script = new InlineScript("doc['keywords']"),
                            Size = 20,
                            Order = orderkeywords,
                        };
                        request.Aggregations.Add("aggkeywords", aggkeywords);

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                   
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;
                                
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.aggs = aggs;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> caseReasonAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "caseReasonAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合

                        List<TermsOrder> ordercaseReason = new List<TermsOrder>();
                        ordercaseReason.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggcaseReason = new TermsAggregation("aggcaseReason")
                        {
                            Field = "caseReason",
                            //Script = new InlineScript("doc['caseReason']"),
                            Size = 2000,
                            Order = ordercaseReason,

                        };
                        request.Aggregations.Add("aggcaseReason", aggcaseReason);
                        
                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                    List<caseReason> crs = _baseDatasHelper.GetcaseReasonOUT(null, null);
                    
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;
                                if (item == "aggcaseReason")
                                {
                                    if (key.Split('-').Count() > 1)
                                    {
                                        caseReason cr = crs.Where(p => p.Id == key.Split('-')[1]).FirstOrDefault();
                                        if (cr != null)
                                        {
                                            cr.count = count;
                                        }
                                    }
                                    else
                                    {
                                        caseReason cr = crs.Where(p => p.text == key).FirstOrDefault();
                                        if (cr != null)
                                        {
                                            cr.count = count;
                                        }
                                    }
                                }
                                else
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    crs = crs.Where(p => p.count > 0).ToList();
                    List<caseReason> crlevels = crs.Where(p => p.levels == 1).ToList();

                    for (int i = 0; i < crlevels.Count; i++)
                    {
                        if (crlevels[i].childCasereasons != null && crlevels[i].childCasereasons.Count > 0)
                        {
                            List<caseReason> crs1 = crlevels[i].childCasereasons.ToList();
                            for (int i1 = 0; i1 < crs1.Count; i1++)
                            {
                                caseReason crt1 = crs.Where(p => p.Id == crs1[i1].Id).FirstOrDefault();
                                if (crt1 != null)
                                {
                                    crs1[i1].count = crt1.count;
                                    if (crs1[i1].childCasereasons != null && crs1[i1].childCasereasons.Count > 0)
                                    {
                                        List<caseReason> crs2 = crs1[i1].childCasereasons.ToList();
                                        for (int i2 = 0; i2 < crs2.Count; i2++)
                                        {
                                            caseReason crt3 = crs.Where(p => p.Id == crs2[i2].Id).FirstOrDefault();
                                            if (crt3 != null)
                                            {
                                                crs2[i2].count = crt3.count;
                                                if (crs2[i2].childCasereasons != null && crs2[i2].childCasereasons.Count > 0)
                                                {
                                                    List<caseReason> crs3 = crs2[i2].childCasereasons.ToList();
                                                    for (int i3 = 0; i3 < crs3.Count; i3++)
                                                    {
                                                        caseReason crt4 = crs.Where(p => p.Id == crs3[i3].Id).FirstOrDefault();
                                                        if (crt4 != null)
                                                        {
                                                            crs3[i3].count = crt4.count;
                                                            if (crs3[i3].childCasereasons != null && crs3[i3].childCasereasons.Count > 0)
                                                            {
                                                                List<caseReason> crs4 = crs3[i3].childCasereasons.ToList();
                                                                for (int i4 = 0; i4 < crs4.Count; i4++)
                                                                {
                                                                    caseReason crt5 = crs.Where(p => p.Id == crs4[i4].Id).FirstOrDefault();
                                                                    if (crt5 != null)
                                                                    {
                                                                        crs4[i4].count = crt5.count;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    for (int i = 0; i < crlevels.Count; i++)
                    {
                        if (crlevels[i].count == null || crlevels[i].count == 0)
                        {
                            crlevels.Remove(crlevels[i]);
                        }
                        else
                        {
                            if (crlevels[i].childCasereasons != null && crlevels[i].childCasereasons.Count > 0)
                            {
                                List<caseReason> crs1 = crlevels[i].childCasereasons.ToList();
                                for (int i1 = 0; i1 < crs1.Count; i1++)
                                {
                                    if (crs1[i1].count == null || crs1[i1].count == 0)
                                    {
                                        crlevels[i].childCasereasons.Remove(crs1[i1]);
                                    }
                                    else
                                    {
                                        if (crs1[i1].childCasereasons != null && crs1[i1].childCasereasons.Count > 0)
                                        {
                                            List<caseReason> crs2 = crs1[i1].childCasereasons.ToList();
                                            for (int i2 = 0; i2 < crs2.Count; i2++)
                                            {
                                                if (crs2[i2].count == null || crs2[i2].count == 0)
                                                {
                                                    crs1[i1].childCasereasons.Remove(crs2[i2]);
                                                }
                                                else
                                                {
                                                    if (crs2[i2].childCasereasons != null && crs2[i2].childCasereasons.Count > 0)
                                                    {
                                                        List<caseReason> crs3 = crs2[i2].childCasereasons.ToList();
                                                        for (int i3 = 0; i3 < crs3.Count; i3++)
                                                        {
                                                            if (crs3[i3].count == null || crs3[i3].count == 0)
                                                            {
                                                                crs2[i2].childCasereasons.Remove(crs3[i3]);
                                                            }
                                                            else
                                                            {
                                                                if (crs3[i3].childCasereasons != null && crs3[i3].childCasereasons.Count > 0)
                                                                {
                                                                    List<caseReason> crs4 = crs3[i3].childCasereasons.ToList();
                                                                    for (int i4 = 0; i4 < crs4.Count; i4++)
                                                                    {
                                                                        if (crs4[i4].count == null || crs4[i4].count == 0)
                                                                        {
                                                                            crs3[i3].childCasereasons.Remove(crs4[i4]);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }
                                                }
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }




                    aggs.agg = agg;
                    aggs.crs = crlevels;



                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.aggs = aggs;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> judgeTypeAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "judgeTypeAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合
                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderjudgeType = new List<TermsOrder>();
                        orderjudgeType.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggjudgeType = new TermsAggregation("aggjudgeType")
                        {
                            Field = "judgeType",
                            //Script = new InlineScript("doc['judgeType']"),
                            Size = 100,
                            Order = orderjudgeType,
                        };
                        request.Aggregations.Add("aggjudgeType", aggjudgeType);
                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderjudgeProcess = new List<TermsOrder>();
                        orderjudgeProcess.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggjudgeProcess = new TermsAggregation("aggjudgeProcess")
                        {
                            Field = "judgeProcess",
                            //Script = new InlineScript("doc['judgeProcess']"),
                            Size = 1000,
                            Order = orderjudgeProcess,
                        };
                        request.Aggregations.Add("aggjudgeProcess", aggjudgeProcess);
                        //////////////////////////////////////////////////////////////////////////////////////////
                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                    List<LawjudgeProcess> ljps = _baseDatasHelper.GetjudgeProcessOUT(null, null);
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;
                                if (item == "aggjudgeType")
                                {
                                    LawjudgeProcess cr = ljps.Where(p => p.name == key).FirstOrDefault();
                                    if (cr != null)
                                    {
                                        cr.count = count;
                                    }
                                }
                                else if (item == "aggjudgeProcess")
                                {
                                    LawjudgeProcess cr = ljps.Where(p => p.name == key).FirstOrDefault();
                                    if (cr != null)
                                    {
                                        cr.count = count;
                                    }
                                }
                                else
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    ljps = ljps.Where(p => p.count > 0).ToList();
                    List<LawjudgeProcess> ljpLevels = ljps.Where(p => p.levels == 1).ToList();
                    for (int i = 0; i < ljpLevels.Count; i++)
                    {
                        if (ljpLevels[i].childLawjudgeProcess != null && ljpLevels[i].childLawjudgeProcess.Count > 0)
                        {
                            List<LawjudgeProcess> ljps1 = ljpLevels[i].childLawjudgeProcess.ToList();
                            for (int i1 = 0; i1 < ljps1.Count; i1++)
                            {
                                LawjudgeProcess crt1 = ljps.Where(p => p.Id == ljps1[i1].Id).FirstOrDefault();
                                if (crt1 != null)
                                {
                                    ljps1[i1].count = crt1.count;
                                    if (ljps1[i1].childLawjudgeProcess != null && ljps1[i1].childLawjudgeProcess.Count > 0)
                                    {
                                        List<LawjudgeProcess> ljps2 = ljps1[i1].childLawjudgeProcess.ToList();
                                        for (int i2 = 0; i2 < ljps2.Count; i2++)
                                        {
                                            LawjudgeProcess crt3 = ljps.Where(p => p.Id == ljps2[i2].Id).FirstOrDefault();
                                            if (crt3 != null)
                                            {
                                                ljps2[i2].count = crt3.count;
                                                if (ljps2[i2].childLawjudgeProcess != null && ljps2[i2].childLawjudgeProcess.Count > 0)
                                                {
                                                    List<LawjudgeProcess> ljps3 = ljps2[i2].childLawjudgeProcess.ToList();
                                                    for (int i3 = 0; i3 < ljps3.Count; i3++)
                                                    {
                                                        LawjudgeProcess crt4 = ljps.Where(p => p.Id == ljps3[i3].Id).FirstOrDefault();
                                                        if (crt4 != null)
                                                        {
                                                            ljps3[i3].count = crt4.count;
                                                            if (ljps3[i3].childLawjudgeProcess != null && ljps3[i3].childLawjudgeProcess.Count > 0)
                                                            {
                                                                List<LawjudgeProcess> ljps4 = ljps3[i3].childLawjudgeProcess.ToList();
                                                                for (int i4 = 0; i4 < ljps4.Count; i4++)
                                                                {
                                                                    LawjudgeProcess crt5 = ljps.Where(p => p.Id == ljps4[i4].Id).FirstOrDefault();
                                                                    if (crt5 != null)
                                                                    {
                                                                        ljps4[i4].count = crt5.count;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    for (int i = 0; i < ljpLevels.Count; i++)
                    {
                        if (ljpLevels[i].count == null || ljpLevels[i].count == 0)
                        {
                            ljpLevels.Remove(ljpLevels[i]);
                        }
                        else
                        {
                            if (ljpLevels[i].childLawjudgeProcess != null && ljpLevels[i].childLawjudgeProcess.Count > 0)
                            {
                                List<LawjudgeProcess> crs1 = ljpLevels[i].childLawjudgeProcess.ToList();
                                for (int i1 = 0; i1 < crs1.Count; i1++)
                                {
                                    if (crs1[i1].count == null || crs1[i1].count == 0)
                                    {
                                        ljpLevels[i].childLawjudgeProcess.Remove(crs1[i1]);
                                    }
                                    else
                                    {
                                        if (crs1[i1].childLawjudgeProcess != null && crs1[i1].childLawjudgeProcess.Count > 0)
                                        {
                                            List<LawjudgeProcess> crs2 = crs1[i1].childLawjudgeProcess.ToList();
                                            for (int i2 = 0; i2 < crs2.Count; i2++)
                                            {
                                                if (crs2[i2].count == null || crs2[i2].count == 0)
                                                {
                                                    crs1[i1].childLawjudgeProcess.Remove(crs2[i2]);
                                                }
                                                else
                                                {
                                                    if (crs2[i2].childLawjudgeProcess != null && crs2[i2].childLawjudgeProcess.Count > 0)
                                                    {
                                                        List<LawjudgeProcess> crs3 = crs2[i2].childLawjudgeProcess.ToList();
                                                        for (int i3 = 0; i3 < crs3.Count; i3++)
                                                        {
                                                            if (crs3[i3].count == null || crs3[i3].count == 0)
                                                            {
                                                                crs2[i2].childLawjudgeProcess.Remove(crs3[i3]);
                                                            }
                                                            else
                                                            {
                                                                if (crs3[i3].childLawjudgeProcess != null && crs3[i3].childLawjudgeProcess.Count > 0)
                                                                {
                                                                    List<LawjudgeProcess> crs4 = crs3[i3].childLawjudgeProcess.ToList();
                                                                    for (int i4 = 0; i4 < crs4.Count; i4++)
                                                                    {
                                                                        if (crs4[i4].count == null || crs4[i4].count == 0)
                                                                        {
                                                                            crs3[i3].childLawjudgeProcess.Remove(crs4[i4]);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }
                                                }
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }


                    aggs.agg = agg;
                    aggs.ljps = ljpLevels;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.aggs = aggs;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        [RemoteService(false)]
        public async Task<RecordSearchOutParamDto> courtFullNameAggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "courtFullNameAggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合

                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordercourtFullName = new List<TermsOrder>();
                        ordercourtFullName.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggcourtFullName = new TermsAggregation("aggcourtFullName")
                        {
                            Field = "courtFullNameAgg",
                            //Script = new InlineScript("doc['courtFullNameAgg'].value"),
                            Size = 1000,
                            Order = ordercourtFullName,
                        };
                        request.Aggregations.Add("aggcourtFullName", aggcourtFullName);
                        ////////////////////////////////////
                        List<TermsOrder> orderregion = new List<TermsOrder>();
                        orderregion.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggregion = new TermsAggregation("aggregion")
                        {
                            Field = "region",
                            //Script = new InlineScript("doc['region']"),
                            Size = 20,
                            Order = orderregion,
                        };
                        request.Aggregations.Add("aggregion", aggregion);

                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                    List<region> re = new List<region>();
                    List<courtFullName> cfs = _baseDatasHelper.GetcourtFullName("", "", 0);
                    List<courtFullName> tempss = new List<courtFullName>();
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;
                                if (item == "aggregion")
                                {
                                    region reg = new region() { count = count, dataValue = key };
                                    re.Add(reg);
                                }
                                else if (item == "aggcourtFullName")
                                {
                                    courtFullName cf = cfs.Where(p => p.name == key).FirstOrDefault();
                                    if (cf != null)
                                    {
                                        cf.count = count;
                                        tempss.Add(cf);

                                    }
                                }
                                else
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    foreach (var item in tempss)
                    {
                        region reg = re.Where(p => p.dataValue == item.region).FirstOrDefault();
                        if (reg != null)
                        {
                            reg.courts.Add(item);
                        }
                    }

                    aggs.agg = agg;
                    aggs.re = re;

                    RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    returnenty.Datas = recordsearchoutdtos;
                    returnenty.aggs = aggs;
                    returnenty.skip = input.skip;
                    returnenty.size = input.size;
                    returnenty.sortorder = input.sortorder;
                    returnenty.Ascending = input.Ascending;
                    returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return (RecordSearchOutParamDto)results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion
        public async Task<object> RecordSearchAsync5AggAsync(RecordSearchParamDto input)
        {
            try
            {
                string cachestr = "RecordSearchAsync5AggAsync" + GetCacheno(input);
                var results = _cacheManager.GetCache("WenShuSearsh").GetOrDefault(cachestr);
                if (results == null || !input.iscache)
                {
                    //关键词高亮
                    var request = new SearchRequest("cprecord")
                    {
                        Size = 0,
                        Aggregations = new AggregationDictionary(), //聚合
                    };

                    //正则表达式查询
                    //RegexpQuery rq = new RegexpQuery();
                    //rq.Field = "description";
                    //rq.Value = "azu.*";
                    //rq.MaximumDeterminizedStates = 20000;


                    if (input.isagg)
                    {
                        #region//聚合

                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> ordercourtFullName = new List<TermsOrder>();
                        ordercourtFullName.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggcourtFullName = new TermsAggregation("aggcourtFullName")
                        {
                            Field = "courtFullNameAgg",
                            //Script = new InlineScript("doc['courtFullNameAgg'].value"),
                            Size = 50,
                            Order = ordercourtFullName,
                        };
                        request.Aggregations.Add("aggcourtFullName", aggcourtFullName);
                        ////////////////////////////////////
                        List<TermsOrder> orderregion = new List<TermsOrder>();
                        orderregion.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });//按数量//aggorder.Add(new TermsOrder() { Key = "_key", Order = Nest.SortOrder.Descending }); //按key
                        AggregationContainer aggregion = new TermsAggregation("aggregion")
                        {
                            Field = "region",
                            //Script = new InlineScript("doc['region']"),
                            Size = 20,
                            Order = orderregion,
                        };
                        request.Aggregations.Add("aggregion", aggregion);
                        //////////////////////////////////////////////////////////////////////////////////////////
                        List<TermsOrder> orderkeywords = new List<TermsOrder>();
                        orderkeywords.Add(new TermsOrder() { Key = "_count", Order = Nest.SortOrder.Descending });
                        AggregationContainer aggkeywords = new TermsAggregation("aggkeywords")
                        {
                            Field = "keywords",
                            //Script = new InlineScript("doc['keywords']"),
                            Size = 20,
                            Order = orderkeywords,
                        };
                        request.Aggregations.Add("aggkeywords", aggkeywords);
                        #endregion
                    }

                    ///////////////////////
                    QueryContainer querycontainer = SearchParem(input);
                    if (querycontainer != null)
                    {
                        request.Query = querycontainer;
                    }



                    var his = await _abpElasticSearch.SearchAsync<RecordSearchOutDTO, string>(request);

                    string hightli = _Helpers.returnChina(input.ToJsonString().Replace(":", "的"));
                    List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                    List<RecordSearchOutDTO> recordsearchoutdtos = new List<RecordSearchOutDTO>();
                    foreach (var item in his.Hits)
                    {
                        RecordSearchOutDTO enty = (RecordSearchOutDTO)item.Source;
                        if (!string.IsNullOrEmpty(enty.Id))
                        {
                            _Helpers.ReturnhightTags(enty, x);
                        }
                        recordsearchoutdtos.Add(enty);
                    }

                    aggs aggs = new aggs();
                    List<region> re = new List<region>();
                    List<courtFullName> cfs = _baseDatasHelper.GetcourtFullName("", "", 0);
                    Dictionary<string, List<Dictionary<string, long?>>> agg = new Dictionary<string, List<Dictionary<string, long?>>>();

                    foreach (var item in his.Aggregations.Keys)
                    {
                        var dd = his.Aggregations.GetValueOrDefault(item);
                        string types = dd.GetType().Name;
                        if (types == "SingleBucketAggregate")
                        {
                            foreach (var sl in ((Nest.SingleBucketAggregate)dd).Values)
                            {
                                List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                                foreach (var ts in ((Nest.BucketAggregate)sl).Items)
                                {
                                    Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                    tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                    templ.Add(tempd);
                                }
                                agg.Add(item, templ);
                            }
                        }
                        else
                        {
                            List<Dictionary<string, long?>> templ = new List<Dictionary<string, long?>>();
                            foreach (var ts in ((Nest.BucketAggregate)dd).Items)
                            {
                                string key = ((Nest.KeyedBucket<object>)ts).Key.ToString();
                                long? count = ((Nest.KeyedBucket<object>)ts).DocCount;

                                Dictionary<string, long?> tempd = new Dictionary<string, long?>();
                                tempd.Add(((Nest.KeyedBucket<object>)ts).Key.ToString(), ((Nest.KeyedBucket<object>)ts).DocCount);
                                templ.Add(tempd);
                            }
                            if (templ.Count > 0)
                            {
                                agg.Add(item, templ);
                            }
                        }


                    }

                    aggs.agg = agg;
                    aggs.re = re;

                    //RecordSearchOutParamDto returnenty = new RecordSearchOutParamDto();
                    //returnenty.Datas = recordsearchoutdtos;
                    //returnenty.aggs = aggs;
                    //returnenty.skip = input.skip;
                    //returnenty.size = input.size;
                    //returnenty.sortorder = input.sortorder;
                    //returnenty.Ascending = input.Ascending;
                    //returnenty.counts = his.Total;//await _abpElasticSearch.CountAsync("cprecord", request.Query);

                    var returnenty = new
                    {
                        aggcourtFullName = agg["aggcourtFullName"].ToList(),
                        aggregion = agg["aggregion"].ToList(),
                        aggkeywords = agg["aggkeywords"].ToList()
                    };


                    if ((querycontainer == null || (string.IsNullOrEmpty(input.Data.name) && string.IsNullOrEmpty(input.Data.docCode)
                        && (input.Data.peoples == null || input.Data.peoples.Count == 0)
                        && (input.Data.gongshujiguan == null || input.Data.gongshujiguan.Count == 0)
                            && (input.Data.judger == null || input.Data.judger.Count == 0)
                            && string.IsNullOrEmpty(input.Data.content))) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(2, 0, 0, 0));
                    }
                    else if ((!string.IsNullOrEmpty(input.Data.name) || !string.IsNullOrEmpty(input.Data.docCode)
                        || (input.Data.peoples != null && input.Data.peoples.Count > 0)
                        || (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                        || (input.Data.judger != null && input.Data.judger.Count > 0)
                        || !string.IsNullOrEmpty(input.Data.content)) && input.skip == 0 && ((Elasticsearch.Net.ApiCallDetails)((Nest.ResponseBase)his).ApiCall).Success)
                    {
                        _cacheManager.GetCache("WenShuSearsh").Set(cachestr, returnenty, new TimeSpan(0, 20, 0, 0));
                    }

                    return returnenty;
                }
                else
                {
                    return results;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public string GetCacheno(RecordSearchParamDto input)
        {
            string tempr = string.Empty;
            if (input.size > 0)
            {
                tempr += "1" + input.size;
            }
            if (input.skip > 0)
            {
                tempr += "2" + input.skip;

            }
            if (input.Ascending > 0)
            {
                tempr += "3" + input.Ascending;

            }
            if (!string.IsNullOrEmpty(input.sortorder))
            {
                tempr += "4" + input.sortorder;

            }


            if (input.Data.year != null && input.Data.year.Count > 0)
            {
                foreach (string item in input.Data.year)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "5" + item;
                    }
                }
            }
            if (input.Data.yearmoth != null && input.Data.yearmoth.Count > 0)
            {
                foreach (string item in input.Data.yearmoth)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "6" + item;
                    }
                }

            }
            if (input.Data.industry != null && input.Data.industry.Count > 0)
            {
                foreach (string item in input.Data.industry)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "7" + item;
                    }
                }

            }
            if (input.Data.judgeSprideD > 0)
            {
                tempr += "8" + input.Data.judgeSprideD;

            }
            if (input.Data.judgeSprideX > 0)
            {
                tempr += "9" + input.Data.judgeSprideX;

            }


            if (input.Data.judgeResults != null && input.Data.judgeResults.Count > 0)
            {
                foreach (string item in input.Data.judgeResults)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "a" + item;
                    }
                }

            }

            if (input.Data.crimeMoneyD > 0)
            {
                tempr += "b" + input.Data.crimeMoneyD;

            }
            if (input.Data.crimeMoneyX > 0)
            {
                tempr += "c" + input.Data.crimeMoneyX;

            }
            if (input.Data.projectMoneyD > 0)
            {
                tempr += "d" + input.Data.projectMoneyD;

            }
            if (input.Data.projectMoneyX > 0)
            {
                tempr += "e" + input.Data.projectMoneyX;

            }
            if (!string.IsNullOrEmpty(input.Data.name))
            {
                tempr += "f" + input.Data.name;

            }
            if (!string.IsNullOrEmpty(input.Data.docCode))
            {
                tempr += "g" + input.Data.docCode;

            }

            if (input.Data.isdocCodeOthers > 0)
            {
                tempr += "h" + input.Data.isdocCodeOthers;

            }

            if (input.Data.peoples != null && input.Data.peoples.Count > 0)
            {
                foreach (string item in input.Data.peoples)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "i" + item;
                    }
                }

            }
            if (input.Data.keywords != null && input.Data.keywords.Count > 0)
            {
                foreach (string item in input.Data.keywords)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "j" + item;
                    }
                }

            }
            if (input.Data.caseReason != null && input.Data.caseReason.Count > 0)
            {
                foreach (string item in input.Data.caseReason)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "k" + item;
                    }
                }
            }
            if (input.Data.lawLists != null && input.Data.lawLists.Count > 0)
            {
                foreach (var item in input.Data.lawLists)
                {
                    if (!string.IsNullOrEmpty(item.fgmc))
                    {
                        tempr += "l" + item.fgmc;
                    }
                    if (!string.IsNullOrEmpty(item.tkt))
                    {
                        tempr += "m" + item.tkt;
                    }
                }
            }
            if (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
            {
                foreach (string item in input.Data.gongshujiguan)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "n" + item;
                    }
                }

            }
            if (input.Data.lvshis != null && input.Data.lvshis.Count > 0)
            {
                foreach (var item in input.Data.lvshis)
                {
                    if (!string.IsNullOrEmpty(item.lvshi))
                    {
                        tempr += "o" + item.lvshi;
                    }
                    if (!string.IsNullOrEmpty(item.lvshishiwusuo))
                    {
                        tempr += "p" + item.lvshishiwusuo;
                    }
                }

            }
            if (input.Data.judgeType != null && input.Data.judgeType.Count > 0)
            {
                foreach (string item in input.Data.judgeType)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "q" + item;
                    }
                }

            }
            if (input.Data.judgeProcess != null && input.Data.judgeProcess.Count > 0)
            {
                foreach (string item in input.Data.judgeProcess)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "r" + item;
                    }
                }

            }
            if (input.Data.judgeDateS != null)
            {
                tempr += "s" + input.Data.judgeDateS.ToString();
            }
            if (input.Data.judgeDateE != null)
            {
                tempr += "t" + input.Data.judgeDateE.ToString();

            }

            if (input.Data.docType != null && input.Data.docType.Count > 0)
            {
                foreach (string item in input.Data.docType)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "u" + item;
                    }
                }
            }
            if (input.Data.proofs != null && input.Data.proofs.Count > 0)
            {
                foreach (string item in input.Data.proofs)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "v" + item;
                    }
                }
            }
            if (input.Data.features != null && input.Data.features.Count > 0)
            {
                foreach (string item in input.Data.features)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "w" + item;
                    }
                }
            }
            if (input.Data.focuss != null && input.Data.focuss.Count > 0)
            {
                foreach (string item in input.Data.focuss)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "x" + item;
                    }
                }
            }
            if (input.Data.prisontermD > 0)
            {
                tempr += "y" + input.Data.prisontermD;

            }
            if (input.Data.prisontermX > 0)
            {
                tempr += "z" + input.Data.prisontermX;

            }

            if (input.Data.mainCriminal != null && input.Data.mainCriminal.Count > 0)
            {
                foreach (string item in input.Data.mainCriminal)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "A" + item;
                    }
                }
            }
            if (input.Data.fuCriminal != null && input.Data.fuCriminal.Count > 0)
            {
                foreach (string item in input.Data.fuCriminal)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "B" + item;
                    }
                }
            }
            if (input.Data.changeCriminal != null && input.Data.changeCriminal.Count > 0)
            {
                foreach (string item in input.Data.changeCriminal)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "C" + item;
                    }
                }
            }
            if (input.Data.noxingCriminal != null && input.Data.noxingCriminal.Count > 0)
            {
                foreach (string item in input.Data.noxingCriminal)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "D" + item;
                    }
                }
            }
            if (input.Data.forfeitD > 0)
            {
                tempr += "E" + input.Data.forfeitD;

            }
            if (input.Data.forfeitX > 0)
            {
                tempr += "F" + input.Data.forfeitX;

            }
            if (input.Data.criminalLaw != null && input.Data.criminalLaw.Count > 0)
            {
                foreach (string item in input.Data.criminalLaw)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "G" + item;
                    }
                }
            }
            if (input.Data.statusCriminal != null && input.Data.statusCriminal.Count > 0)
            {
                foreach (string item in input.Data.statusCriminal)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "H" + item;
                    }
                }
            }
            if (input.Data.disadvantage != null && input.Data.disadvantage.Count > 0)
            {
                foreach (string item in input.Data.disadvantage)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "I" + item;
                    }
                }
            }
            if (input.Data.courtFullName != null && input.Data.courtFullName.Count > 0)
            {
                foreach (string item in input.Data.courtFullName)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "J" + item;
                    }
                }
            }
            if (input.Data.region != null && input.Data.region.Count > 0)
            {
                foreach (string item in input.Data.region)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "K" + item;
                    }
                }
            }
            if (input.Data.judger != null && input.Data.judger.Count > 0)
            {
                foreach (string item in input.Data.judger)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "L" + item;
                    }
                }
            }
            if (input.Data.courtLevel != null && input.Data.courtLevel.Count > 0)
            {
                foreach (string item in input.Data.courtLevel)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        tempr += "M" + item;
                    }
                }
            }

            if (!string.IsNullOrEmpty(input.Data.content))
            {
                tempr += "N" + input.Data.content;

            }


            return tempr;

        }

        private QueryContainer SearchParem(RecordSearchParamDto input)
        {
            QueryContainer request = new QueryContainer();
            bool isnull = true;
            if (input != null && input.Data != null)
            {
                if (input.Data.year != null && input.Data.year.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.year)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("year"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.courtLevel != null && input.Data.courtLevel.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.courtLevel)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("courtLevel"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                


                if (input.Data.yearmoth != null && input.Data.yearmoth.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.yearmoth)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("yearmoth"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.industry != null && input.Data.industry.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.industry)
                    {
                        qc = qc || new NestedQuery()
                        {
                            Path = "companys",
                            Query = new TermQuery
                            {
                                Field = new Field("companys.industry"),
                                Value = item.Replace(" ", "")
                            }
                        };
                    }
                    request = request && (qc);
                }

                if (input.Data.judgeSprideD > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("judgeSpride"),
                        LessThanOrEqualTo = input.Data.judgeSprideD
                    };
                }
                if (input.Data.judgeSprideX > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("judgeSpride"),
                        GreaterThanOrEqualTo = input.Data.judgeSprideX
                    };
                }
                if (input.Data.judgeResults != null && input.Data.judgeResults.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.judgeResults)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("judgeResults"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.crimeMoneyD > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("crimeMoney"),
                        LessThanOrEqualTo = input.Data.crimeMoneyD
                    };
                }
                if (input.Data.crimeMoneyX > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("crimeMoney"),
                        GreaterThanOrEqualTo = input.Data.crimeMoneyX
                    };
                }
                if (input.Data.projectMoneyD > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("projectMoney"),
                        LessThanOrEqualTo = input.Data.projectMoneyD
                    };
                }
                if (input.Data.projectMoneyX > 0)
                {
                    isnull = false;
                    request = request &&
                    new LongRangeQuery()
                    {
                        Field = new Field("projectMoney"),
                        GreaterThanOrEqualTo = input.Data.projectMoneyX
                    };
                }
                
                if (!string.IsNullOrEmpty(input.Data.docCode))
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    if (input.Data.isdocCodeOthers == 1)
                    {
                        //qc = qc || new MatchQuery
                        //{
                        //    Query = input.Data.docCode,
                        //    Field = new Field("docCodeOthersstr"),
                        //    Operator = Operator.And,
                        //    Analyzer = "ik_max_word"
                        //};
                        qc = qc || new TermQuery
                        {
                            Field = new Field("docCodeOthers"),
                            Value = input.Data.docCode.Replace(" ", "")
                        };
                        
                    }
                    else
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("docCode"),
                            Value = input.Data.docCode.Replace(" ", "")
                        };
                    }
                    
                    request = request && (qc);

                }
                //if (input.Data.docCodeOthers != null && input.Data.docCodeOthers.Count > 0)
                //{
                //    isnull = false;
                //    QueryContainer qc = new QueryContainer();
                    
                //    else
                //    {
                //        foreach (var item in input.Data.docCodeOthers)
                //        {
                //            qc = qc || new TermQuery
                //            {
                //                Field = new Field("docCodeOthers"),
                //                Value = item.Replace(" ", "")
                //            };
                //        }
                //    }
                //    request = request && (qc);

                //}
                if (input.Data.peoples != null && input.Data.peoples.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.peoples)
                    {
                        //qc = qc || new MatchQuery
                        //{
                        //    Query = item,
                        //    Field = new Field("peoplestr"),
                        //    Operator = Operator.And,
                        //    Analyzer = "ik_max_word"
                        //};
                        qc = qc || new TermQuery
                        {
                            Field = new Field("peoples"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.keywords != null && input.Data.keywords.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.keywords)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("keywords"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.caseReason != null && input.Data.caseReason.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.caseReason)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("caseReason"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.lawLists != null && input.Data.lawLists.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.lawLists)
                    {
                        if (!string.IsNullOrEmpty(item.fgmc) && !string.IsNullOrEmpty(item.tkt))
                        {
                            string Alianame = item.fgmc.Replace("(", "（").Replace(")", "）").Replace(" ", "");
                            Alianame = Regex.Replace(Alianame, "（.*?）", "");

                            qc = qc || (new NestedQuery()
                            {
                                Path = "lawLists",
                                Query = new MatchQuery
                                {
                                    Query = Alianame,
                                    Field = new Field("lawLists.fgmc"),
                                    Operator = Operator.And,
                                    Analyzer = "ik_max_word"
                                }
                            } && new NestedQuery()
                            {
                                Path = "lawLists",
                                Query = new TermQuery
                                {
                                    Field = new Field("lawLists.tkt"),
                                    Value = item.tkt.Replace(" ", "")
                                }
                            });
                        }
                        else if (!string.IsNullOrEmpty(item.fgmc))
                        {
                            string Alianame = item.fgmc.Replace("(", "（").Replace(")", "）").Replace(" ", "");
                            Alianame = Regex.Replace(Alianame, "（.*?）", "");
                            qc = qc || new NestedQuery()
                            {
                                Path = "lawLists",
                                Query = new MatchQuery
                                {
                                    Query = Alianame,
                                    Field = new Field("lawLists.fgmc"),
                                    Operator = Operator.And,
                                    Analyzer = "ik_max_word"
                                }
                            };
                        }
                    }
                    request = request && (qc);
                }

                if (input.Data.gongshujiguan != null && input.Data.gongshujiguan.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.gongshujiguan)
                    {
                        qc = qc || new MatchQuery
                        {
                            Query = item,
                            Field = new Field("peoplestr"),
                            Operator = Operator.And,
                            Analyzer = "ik_max_word"
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.lvshis != null && input.Data.lvshis.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.lvshis)
                    {
                        //qc = qc || new MatchQuery
                        //{
                        //    Query = item.lvshi + "-" + item.lvshishiwusuo,
                        //    Field = new Field("peoplestr"),
                        //    Operator = Operator.And,
                        //    Analyzer = "ik_max_word"
                        //};
                        if (!string.IsNullOrEmpty(item.lvshi) && !string.IsNullOrEmpty(item.lvshishiwusuo))
                        {
                            qc = qc || (new NestedQuery()
                            {
                                Path = "lvs",
                                Query = new TermQuery
                                {
                                    Field = new Field("lvs.lvshi"),
                                    Value = item.lvshi.Replace(" ", "")
                                }
                            } && new NestedQuery()
                            {
                                Path = "lvs",
                                Query = new TermQuery
                                {
                                    Field = new Field("lvs.lvshishiwusuo"),
                                    Value = item.lvshishiwusuo.Replace(" ", "")
                                }
                            });
                        }
                        else if (!string.IsNullOrEmpty(item.lvshi))
                        {
                            qc = qc || (new NestedQuery()
                            {
                                Path = "lvs",
                                Query = new TermQuery
                                {
                                    Field = new Field("lvs.lvshi"),
                                    Value = item.lvshi.Replace(" ", "")
                                }
                            });
                        }
                        else if (!string.IsNullOrEmpty(item.lvshishiwusuo))
                        {
                            qc = qc || (new NestedQuery()
                            {
                                Path = "lvs",
                                Query = new TermQuery
                                {
                                    Field = new Field("lvs.lvshishiwusuo"),
                                    Value = item.lvshishiwusuo.Replace(" ", "")
                                }
                            });
                        }
                    }
                    request = request && (qc);
                }
                if (input.Data.judgeType != null && input.Data.judgeType.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.judgeType)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("judgeType"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.judgeProcess != null && input.Data.judgeProcess.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.judgeProcess)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("judgeProcess"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.judgeDateS != null)
                {
                    isnull = false;
                    request = request &&
                    new DateRangeQuery()
                    {
                        Field = new Field("judgeDate"),
                        GreaterThanOrEqualTo = input.Data.judgeDateS
                    };
                }
                if (input.Data.judgeDateE != null)
                {
                    isnull = false;
                    request = request &&
                    new DateRangeQuery()
                    {
                        Field = new Field("judgeDate"),
                        LessThanOrEqualTo = input.Data.judgeDateE
                    };
                }
                if (input.Data.docType != null && input.Data.docType.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.docType)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("docType"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.proofs != null && input.Data.proofs.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.proofs)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("proofs"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.features != null && input.Data.features.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.features)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("features"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.focuss != null && input.Data.focuss.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.focuss)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("focuss"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.prisontermD > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    qc = qc || new LongRangeQuery()
                    {
                        Field = new Field("prisonterm"),
                        LessThanOrEqualTo = input.Data.prisontermD
                    };
                    request = request && (qc);
                }
                if (input.Data.prisontermX > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    qc = qc || new LongRangeQuery()
                    {
                        Field = new Field("prisonterm"),
                        GreaterThanOrEqualTo = input.Data.prisontermX
                    };
                    request = request && (qc);
                }
                if (input.Data.mainCriminal != null && input.Data.mainCriminal.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.mainCriminal)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("mainCriminal"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.fuCriminal != null && input.Data.fuCriminal.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.fuCriminal)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("fuCriminal"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.changeCriminal != null && input.Data.changeCriminal.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.changeCriminal)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("changeCriminal"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.noxingCriminal != null && input.Data.noxingCriminal.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.noxingCriminal)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("noxingCriminal"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && qc;
                }
                if (input.Data.forfeitD > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    qc = qc || new LongRangeQuery()
                    {
                        Field = new Field("forfeit"),
                        LessThanOrEqualTo = input.Data.forfeitD
                    };
                    request = request && (qc);
                }
                if (input.Data.forfeitX > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    qc = qc || new LongRangeQuery()
                    {
                        Field = new Field("forfeit"),
                        GreaterThanOrEqualTo = input.Data.forfeitX
                    };
                    request = request && (qc);
                }
                if (input.Data.criminalLaw != null && input.Data.criminalLaw.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.criminalLaw)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("criminalLaw"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.statusCriminal != null && input.Data.statusCriminal.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.statusCriminal)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("statusCriminal"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.disadvantage != null && input.Data.disadvantage.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.disadvantage)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("disadvantage"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.courtFullName != null && input.Data.courtFullName.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.courtFullName)
                    {
                        qc = qc || new MatchQuery
                        {
                            Query = item,
                            Field = new Field("courtFullName"),
                            Operator = Operator.And,
                            Analyzer = "ik_max_word"
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.region != null && input.Data.region.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.region)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("region"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }
                if (input.Data.judger != null && input.Data.judger.Count > 0)
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    foreach (var item in input.Data.judger)
                    {
                        qc = qc || new TermQuery
                        {
                            Field = new Field("judgeZ"),
                            Value = item.Replace(" ", "")
                        } || new TermQuery
                        {
                            Field = new Field("judges"),
                            Value = item.Replace(" ", "")
                        };
                    }
                    request = request && (qc);
                }

                if (!string.IsNullOrEmpty(input.Data.name) && !string.IsNullOrEmpty(input.Data.content))
                {
                    isnull = false;
                    request = request &&
                    (new MatchQuery
                    {
                        Query = input.Data.name,
                        Field = new Field("name"),
                        Operator = Operator.And,
                        Analyzer = "ik_max_word"
                    } || new MatchQuery
                    {
                        Query = input.Data.content,
                        Field = new Field("contentstr"),
                        Operator = Operator.And,
                        Analyzer = "ik_max_word"
                    });
                }
                else if (!string.IsNullOrEmpty(input.Data.name))
                {
                    isnull = false;
                    request = request &&
                    new MatchQuery
                    {
                        Query = input.Data.name,
                        Field = new Field("name"),
                        Operator = Operator.And,
                        Analyzer = "ik_max_word"
                    };
                }
                else if (!string.IsNullOrEmpty(input.Data.content))
                {
                    isnull = false;
                    QueryContainer qc = new QueryContainer();
                    qc = qc || new MatchQuery
                    {
                        Query = input.Data.content,
                        Field = new Field("contentstr"),
                        Operator = Operator.And,
                        Analyzer = "ik_max_word"
                    };
                    request = request && (qc);
                }

                if (isnull)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
            return request;
        }
        /// <summary>
        /// 文书详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="hightTags"></param>
        /// <returns></returns>
        public async Task<RecordDetailDTO> GetRecordDetail(string id, string hightTags = null)
        {
            try
            {
                //string ss = "依照《中华人民共和国合同法》二百零六条第四项,第二百零七条、多多《最高人民法院关于适用〈中华人民共和国民事诉讼法〉的解释》第二十七条.第一百七十条第一款第一项，手动阀《中华人民共和国民事诉讼法》第一百四十四条之规定阀手动阀依照《中华人民共和国民事诉讼法》第一条，《中华人民共和国合同法》第二百零六条第四项、第二百零七条、第二十七条，第一百七十条第一款第一项，、第二十七条、第二十七条、第二十七条、第二十七条、第一百四十四条之规定阀手动阀";
                //string sss = new Helpers().returnLawsTag(ss, "<a", "</a>");
                QueryContainer qc = new QueryContainer();
                Id id1 = new Id(id);
                List<Id> ids = new List<Id>();
                ids.Add(id1);
                qc = qc || new IdsQuery
                {
                    Values = ids
                };
                var request = new SearchRequest("cprecord")
                {
                    Query = qc
                };
                var his = await _abpElasticSearch.SearchAsync<RecordDetailDTO, string>(request);

                hightTags = hightTags == null ? "" : System.Web.HttpUtility.UrlDecode(hightTags);
                string hightli = _Helpers.returnChina(hightTags.Replace(":", "的"));
                List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(hightli).Pretty()).Tokens.Select(p => p.Token).ToList();
                foreach (var item in his.Hits)
                {
                    RecordDetailDTO enty = (RecordDetailDTO)item.Source;
                    if (!string.IsNullOrEmpty(enty.Id))
                    {
                        wenshuJM cwenshuJM = new wenshuJM()
                        {
                            courtFullName = enty.courtFullName,
                            docCode = enty.docCode,
                            Id = enty.Id,
                            docCodeOthers = enty.docCodeOthers,
                            judgeDate = enty.judgeDate,
                            judgeProcess = enty.judgeProcess,
                            judgeResult = enty.judgeResult,
                            name = enty.name
                        };
                        _Helpers.ReturnDetailTags(enty, x);
                        _Helpers.ReturnDetailLaw(enty);
                        string preTags8 = "<a class=\"courtFullNames\" >";
                        string postTags8 = "</a>";
                        enty.courtFullName = preTags8 + enty.courtFullName + postTags8;
                        List<string> d = new List<string>();
                        List<wenshuJM> ls = new List<wenshuJM>();
                        d.Add(enty.docCode);
                        GetdocCodeOthersAsync(enty.docCodeOthers, ref d,ref ls);
                        enty.docCodeOthersDetail = ls;
                        enty.docCodeOthersDetail.Add(cwenshuJM);
                        return enty;
                    }
                }


                return new RecordDetailDTO();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取文书简介
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<wenshuJM> GetRecordDetailBy(RecordSearchParamDto input)
        {
            try
            {
                string[] highField = new string[] { "name", "judgeDate", "judgeProcess", "courtFullName", "docCode", "docCodeOthers", "judgeResult" };
                if (input.includeFields == null)
                {
                    input.includeFields = highField;
                }
                var request = new SearchRequest("cprecord")
                {
                    Source = new SourceFilter()
                    {
                        Includes = input.includeFields
                    },
                    Query = SearchParem(input)
                };
                var his = _abpElasticSearch.Search<wenshuJM, string>(request);
                List<wenshuJM> ls = new List<wenshuJM>();
                foreach (var item in his.Hits)
                {
                    wenshuJM enty = (wenshuJM)item.Source;
                    ls.Add(enty);
                }
                return ls;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [RemoteService(false)]
        public void GetdocCodeOthersAsync(List<string> docCode ,ref List<string> docCodeed,ref List<wenshuJM> wjs)
        {
            if (docCode != null)
            {
                foreach (var item in docCode)
                {
                    if (!docCodeed.Contains(item))
                    {
                        docCodeed.Add(item);
                        RecordSearchParamDto input = new RecordSearchParamDto();
                        input.Data = new RecordSearchInputDTO();
                        input.Data.docCode = item;
                        input.Data.isdocCodeOthers = 1;
                        List<wenshuJM> js = GetRecordDetailBy(input);
                        foreach (var j in js)
                        {
                            if (wjs.Where(p=>p.docCode == j.docCode).Count() == 0)
                            {
                                wjs.Add(j);
                            }
                            GetdocCodeOthersAsync(j.docCodeOthers, ref docCodeed, ref wjs);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// docCode获取关联文书
        /// </summary>
        /// <param name="docCode"></param>
        /// <returns></returns>
        public List<wenshuJM> GetdocCodeOthersAll(List<string> docCode)
        {
            List<string> d = new List<string>();
            List<wenshuJM> tmpr = new List<wenshuJM>();
            GetdocCodeOthersAsync(docCode, ref d,ref tmpr);
            tmpr = tmpr.OrderByDescending(p=>p.judgeDate).ToList();
            return tmpr;
        }



        public AnalysisParam AnalysisText(string text)
        {
            AnalysisParam analysisparam = new AnalysisParam();
            analysisparam.Money = _Helpers.getMaxmoney(text);


            List<string> keywords = _baseDatasHelper.getkeywords(9999);
            List<caseReason> getcasereasons = _baseDatasHelper.GetcaseReason();
            List<string> getproofs = _baseDatasHelper.getproofs();
            List<string> getfeatures = _baseDatasHelper.getFeatures();
            List<string> getfocuss = _baseDatasHelper.getfocuss();
            List<courtFullName> getcourtfullname = _baseDatasHelper.GetcourtFullName();
            List<region> getregion = _baseDatasHelper.Getregion();

            List<string> x = _abpElasticSearch.EsClient.Indices.Analyze(x => x.Analyzer("ik_smart").Text(text.Replace("公司","")).Pretty()).Tokens.Select(p => p.Token).ToList();

            foreach (var item in x.Where(p=>p.Length > 1))
            {
                foreach (var k in keywords.Where(p => p.Length > 1))
                {
                    if (k.Contains(item) || item.Contains(k))
                    {
                        analysisparam.keywords.Add(k);
                    }
                }
                foreach (var c in getcasereasons.Where(p => p.text.Length > 1))
                {
                    if (c.text.Contains(item) || item.Contains(c.text))
                    {
                        analysisparam.caseReason.Add(c.text);
                    }
                }
                foreach (var k in getproofs.Where(p => p.Length > 1))
                {
                    if (k.Contains(item) || item.Contains(k))
                    {
                        analysisparam.proofs.Add(k);
                    }
                }
                foreach (var k in getfeatures.Where(p => p.Length > 1))
                {
                    if (k.Contains(item) || item.Contains(k))
                    {
                        analysisparam.features.Add(k);
                    }
                }
                foreach (var k in getfocuss.Where(p => p.Length > 1))
                {
                    if (k.Contains(item) || item.Contains(k))
                    {
                        analysisparam.focuss.Add(k);
                    }
                }
                
                foreach (var k in getregion.Where(p => p.dataValue.Length > 1))
                {
                    if (k.dataValue.Contains(item))
                    {
                        analysisparam.region.Add(k.dataValue);
                    }
                }

                if (analysisparam.region.Count > 0)
                {
                    foreach (var r in analysisparam.region)
                    {
                        foreach (var k in getcourtfullname.Where(p => p.region == r))
                        {
                            if (k.name.Replace("人民", "").Replace("法院", "").Contains(item))
                            {
                                analysisparam.courtFullName.Add(k.name);
                            }
                        }
                    }
                    
                }
                else
                {
                    foreach (var k in getcourtfullname.Where(p => p.name.Length > 1))
                    {
                        if (k.name.Replace("人民", "").Replace("法院", "").Contains(item))
                        {
                            analysisparam.courtFullName.Add(k.name);
                        }
                    }
                }
            }

            analysisparam.keywords = analysisparam.keywords.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p=>p.text).ToList();
            analysisparam.caseReason = analysisparam.caseReason.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();
            analysisparam.proofs = analysisparam.proofs.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();
            analysisparam.features = analysisparam.features.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();
            analysisparam.focuss = analysisparam.focuss.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();
            analysisparam.courtFullName = analysisparam.courtFullName.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();
            analysisparam.region = analysisparam.region.GroupBy(p => p).Select(g => new { text = g.Key, count = g.Count() }).OrderByDescending(p => p.count).Take(4).Select(p => p.text).ToList();

            return analysisparam;
        }


        public class AnalysisParam
        {
            public AnalysisParam()
            {
                keywords = new List<string>();
                caseReason = new List<string>();
                proofs = new List<string>();
                features = new List<string>();
                focuss = new List<string>();
                courtFullName = new List<string>();
                region = new List<string>();
                judger = new List<string>();
            }
            
            public double? Money { get; set; } = 0; //
            
            public List<string>? keywords { get; set; }//标签
           
            public List<string>? caseReason { get; set; }//案由
            
            public List<string>? proofs { get; set; }  //证据
            
            public List<string>? features { get; set; }  //案件特征
            
            public List<string>? focuss { get; set; }  //争议焦点
            
            public List<string>? courtFullName { get; set; }  // 吉林省延吉市人民法院
            
            public List<string>? region { get; set; }  // 吉林省
            
            public List<string>? judger { get; set; }
            
        }
    }
}
